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