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