remove quiet option; we do not use it anyway
[bdrem.git] / src / bdrem / UserInterface.php
1 <?php
2 namespace bdrem;
3
4 abstract class UserInterface
5 {
6     protected $config;
7
8     public function run()
9     {
10         try {
11             $this->config = new Config();
12             $this->config->load();
13             setlocale(LC_TIME, $this->config->locale);
14
15             $parser = $this->loadParameters();
16             $this->parseParameters($parser);
17
18             if (!$this->config->cfgFileExists) {
19                 throw new \Exception('No config file found');
20             }
21
22             $source = $this->config->loadSource();
23             $arEvents = $source->getEvents(
24                 $this->config->date,
25                 $this->config->daysPrev, $this->config->daysNext
26             );
27             usort($arEvents, '\\bdrem\\Event::compare');
28             $this->render($arEvents);
29         } catch (\Exception $e) {
30             $this->preRenderParameterError();
31             echo 'Exception: ' . $e->getCode() . ' - ' . $e->getMessage() . "\n";
32             exit(1);
33         }
34     }
35
36     protected function loadParameters()
37     {
38         $parser = new \Console_CommandLine();
39         $parser->description = 'Birthday reminder';
40         $parser->version = '0.1.0';
41
42         $parser->addOption(
43             'daysNext',
44             array(
45                 'short_name'  => '-n',
46                 'long_name'   => '--days-next',
47                 'description' => 'Show NUM days after date',
48                 'help_name'   => 'NUM',
49                 'action'      => 'StoreInt',
50                 'default'     => $this->config->daysNext,
51             )
52         );
53         $parser->addOption(
54             'daysPrev',
55             array(
56                 'short_name'  => '-p',
57                 'long_name'   => '--previous',
58                 'description' => 'Show NUM days before date',
59                 'help_name'   => 'NUM',
60                 'action'      => 'StoreInt',
61                 'default'     => $this->config->daysPrev,
62             )
63         );
64         $parser->addOption(
65             'renderer',
66             array(
67                 'short_name'  => '-r',
68                 'long_name'   => '--renderer',
69                 'description' => 'Output mode',
70                 'action'      => 'StoreString',
71                 'choices'     => array(
72                     'console',
73                     'html',
74                     'htmltable',
75                     'mail',
76                 ),
77                 'default'     => 'console',
78                 'add_list_option' => true,
79             )
80         );
81         $parser->addOption(
82             'stopOnEmpty',
83             array(
84                 'short_name'  => '-e',
85                 'long_name'   => '--stoponempty',
86                 'description' => 'Output nothing when list is empty',
87                 'action'      => 'StoreTrue',
88                 'default'     => false
89             )
90         );
91         $parser->addOption(
92             'date',
93             array(
94                 'short_name'  => '-d',
95                 'long_name'   => '--date',
96                 'description' => 'Date to show events for',
97                 'action'      => 'StoreString'
98             )
99         );
100         return $parser;
101     }
102
103     protected function parseParameters($parser)
104     {
105         try {
106             $result = $parser->parse();
107             // do something with the result object
108             $this->config->daysNext    = $result->options['daysNext'];
109             $this->config->daysPrev    = $result->options['daysPrev'];
110             $this->config->renderer    = $result->options['renderer'];
111             $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
112             $this->config->setDate($result->options['date']);
113         } catch (\Exception $exc) {
114             $this->preRenderParameterError();
115             $parser->displayError($exc->getMessage());
116         }
117     }
118
119     protected function render($arEvents)
120     {
121         $r = $this->getRenderer();
122         $r->config = $this->config;
123
124         if ($this->config->stopOnEmpty && count($arEvents) == 0) {
125             $r->handleStopOnEmpty();
126             return;
127         }
128         $r->renderAndOutput($arEvents);
129     }
130
131     protected function getRenderer()
132     {
133         $renderer = ucfirst($this->config->renderer);
134         if ($renderer == 'Htmltable') {
135             $renderer = 'HtmlTable';
136         }
137         $class = '\\bdrem\\Renderer_' . $renderer;
138         return new $class();
139     }
140
141     protected function preRenderParameterError()
142     {
143     }
144 }
145 ?>