8fa001ffcb81b5093b50082ea43b28ac20e13ca4
[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         $parser->addOption(
101             'quiet',
102             array(
103                 'short_name'  => '-q',
104                 'long_name'   => '--quiet',
105                 'description' => "Don't print status messages to stdout",
106                 'action'      => 'StoreTrue'
107             )
108         );
109         return $parser;
110     }
111
112     protected function parseParameters($parser)
113     {
114         try {
115             $result = $parser->parse();
116             // do something with the result object
117             $this->config->daysNext    = $result->options['daysNext'];
118             $this->config->daysPrev    = $result->options['daysPrev'];
119             $this->config->renderer    = $result->options['renderer'];
120             $this->config->quiet       = $result->options['quiet'];
121             $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
122             $this->config->setDate($result->options['date']);
123         } catch (\Exception $exc) {
124             $this->preRenderParameterError();
125             $parser->displayError($exc->getMessage());
126         }
127     }
128
129     protected function render($arEvents)
130     {
131         $r = $this->getRenderer();
132         $r->config = $this->config;
133
134         if ($this->config->stopOnEmpty && count($arEvents) == 0) {
135             $r->handleStopOnEmpty();
136             return;
137         }
138         $r->renderAndOutput($arEvents);
139     }
140
141     protected function getRenderer()
142     {
143         $renderer = ucfirst($this->config->renderer);
144         if ($renderer == 'Htmltable') {
145             $renderer = 'HtmlTable';
146         }
147         $class = '\\bdrem\\Renderer_' . $renderer;
148         return new $class();
149     }
150
151     protected function preRenderParameterError()
152     {
153     }
154 }
155 ?>