daysAfter -> daysNext, daysBefore -> daysPrev
[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         $this->config = new Config();
11         $this->config->load();
12         $this->config->date = date('Y-m-d');
13         setlocale(LC_TIME, $this->config->locale);
14         $source = $this->config->loadSource();
15
16         $parser = $this->loadParameters();
17         $this->parseParameters($parser);
18
19         $arEvents = $source->getEvents(
20             $this->config->date,
21             $this->config->daysPrev, $this->config->daysNext
22         );
23         usort($arEvents, '\\bdrem\\Event::compare');
24         $this->render($arEvents);
25     }
26
27     protected function loadParameters()
28     {
29         $parser = new \Console_CommandLine();
30         $parser->description = 'Birthday reminder';
31         $parser->version = '0.1.0';
32
33         $parser->addOption(
34             'daysNext',
35             array(
36                 'short_name'  => '-n',
37                 'long_name'   => '--days-next',
38                 'description' => 'Show NUM days after date',
39                 'help_name'   => 'NUM',
40                 'action'      => 'StoreInt',
41                 'default'     => $this->config->daysAfter,
42             )
43         );
44         $parser->addOption(
45             'daysPrev',
46             array(
47                 'short_name'  => '-p',
48                 'long_name'   => '--previous',
49                 'description' => 'Show NUM days before date',
50                 'help_name'   => 'NUM',
51                 'action'      => 'StoreInt',
52                 'default'     => $this->config->daysBefore,
53             )
54         );
55         $parser->addOption(
56             'renderer',
57             array(
58                 'short_name'  => '-r',
59                 'long_name'   => '--renderer',
60                 'description' => 'Output mode',
61                 'action'      => 'StoreString',
62                 'choices'     => array(
63                     'console',
64                     'html',
65                     'htmltable',
66                     'mail',
67                 ),
68                 'default'     => 'console',
69                 'add_list_option' => true,
70             )
71         );
72         $parser->addOption(
73             'quiet',
74             array(
75                 'short_name'  => '-q',
76                 'long_name'   => '--quiet',
77                 'description' => "Don't print status messages to stdout",
78                 'action'      => 'StoreTrue'
79             )
80         );
81         return $parser;
82     }
83
84     protected function parseParameters($parser)
85     {
86         try {
87             $result = $parser->parse();
88             // do something with the result object
89             $this->config->daysNext = $result->options['daysNext'];
90             $this->config->daysPrev = $result->options['daysPrev'];
91             $this->config->renderer = $result->options['renderer'];
92             $this->config->quiet    = $result->options['quiet'];
93         } catch (\Exception $exc) {
94             $this->preRenderParameterError();
95             $parser->displayError($exc->getMessage());
96         }
97     }
98
99     protected function render($arEvents)
100     {
101         $r = $this->getRenderer();
102         $r->config = $this->config;
103         $r->renderAndOutput($arEvents);
104     }
105
106     protected function getRenderer()
107     {
108         $renderer = ucfirst($this->config->renderer);
109         if ($renderer == 'Htmltable') {
110             $renderer = 'HtmlTable';
111         }
112         $class = '\\bdrem\\Renderer_' . $renderer;
113         return new $class();
114     }
115
116     protected function preRenderParameterError()
117     {
118     }
119 }
120 ?>