5fdde92b98e08a7ef09cf9de4fe0ce376faff9ad
[auerswald-callnotifier.git] / src / callnotifier / CLI.php
1 <?php
2 namespace callnotifier;
3
4
5 class CLI
6 {
7     protected $cliParser;
8     protected $config;
9
10     public function __construct()
11     {
12         $this->setupCli();
13     }
14
15     public function run()
16     {
17         $this->config = new Config();
18         try {
19             $result = $this->cliParser->parse();
20         } catch (\Exception $exc) {
21             $this->cliParser->displayError($exc->getMessage());
22         }
23
24         $this->fillConfig($this->config, $result);
25
26         $log = new Log();
27         if ($result->options['debug'] || $result->options['debugEdss1']) {
28             $debugLogger = new Logger_Debug();
29             $log->addLogger($debugLogger, '*');
30             if ($result->options['debugEdss1']) {
31                 $debugLogger->edss1MsgOnly = true;
32             }
33         }
34
35         $callMonitor = new CallMonitor($this->config, $log);
36
37         $handler = new MessageHandler($this->config, $log, $callMonitor);
38
39         if ($this->config->replayFile !== null) {
40             $sourceClass = 'callnotifier\Source_File';
41         } else {
42             $sourceClass = 'callnotifier\Source_Remote';
43         }
44         $source = new $sourceClass($this->config, $handler);
45         $source->run();
46     }
47
48     public function setupCli()
49     {
50         $p = new \Console_CommandLine();
51         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
52         $p->version = '0.0.1';
53
54         $p->addOption(
55             'host',
56             array(
57                 'short_name'  => '-h',
58                 'long_name'   => '--host',
59                 'description' => 'IP of COMpact 3000',
60                 'help_name'   => 'IP',
61                 'action'      => 'StoreString'
62             )
63         );
64
65         $p->addOption(
66             'dumpFile',
67             array(
68                 'long_name'   => '--dump',
69                 'description' => 'dump messages into file for later replay',
70                 'help_name'   => 'FILE',
71                 'action'      => 'StoreString'
72             )
73         );
74         $p->addOption(
75             'replayFile',
76             array(
77                 'long_name'   => '--replay',
78                 'description' => "Replay messages from file instead from network",
79                 'help_name'   => 'FILE',
80                 'action'      => 'StoreString'
81             )
82         );
83
84         $p->addOption(
85             'debug',
86             array(
87                 'short_name'  => '-d',
88                 'long_name'   => '--debug',
89                 'description' => "Debug mode: Echo all received messages and events",
90                 'action'      => 'StoreTrue'
91             )
92         );
93         $p->addOption(
94             'debugEdss1',
95             array(
96                 'short_name'  => '-e',
97                 'long_name'   => '--debug-edss1',
98                 'description' => "Debug mode: Show EDSS1 messages only",
99                 'action'      => 'StoreTrue'
100             )
101         );
102
103         $this->cliParser = $p;
104     }
105
106     protected function fillConfig($config, $result)
107     {
108         $config->setIfNotEmpty('host', $result->options['host']);
109         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
110         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
111     }
112 }
113
114 ?>