2a6d3ca6ea4aa5d96c233ff5d2ff28ae235ef8a8
[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         $log->addLogger(
35             new Logger_CallEcho(), array('startingCall', 'finishedCall')
36         );
37
38         $callMonitor = new CallMonitor($this->config, $log);
39
40         $handler = new MessageHandler($this->config, $log, $callMonitor);
41
42         if ($this->config->replayFile !== null) {
43             $sourceClass = 'callnotifier\Source_File';
44         } else {
45             $sourceClass = 'callnotifier\Source_Remote';
46         }
47         $source = new $sourceClass($this->config, $handler);
48         $source->run();
49     }
50
51     public function setupCli()
52     {
53         $p = new \Console_CommandLine();
54         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
55         $p->version = '0.0.1';
56
57         $p->addOption(
58             'host',
59             array(
60                 'short_name'  => '-h',
61                 'long_name'   => '--host',
62                 'description' => 'IP of COMpact 3000',
63                 'help_name'   => 'IP',
64                 'action'      => 'StoreString'
65             )
66         );
67
68         $p->addOption(
69             'dumpFile',
70             array(
71                 'long_name'   => '--dump',
72                 'description' => 'dump messages into file for later replay',
73                 'help_name'   => 'FILE',
74                 'action'      => 'StoreString'
75             )
76         );
77         $p->addOption(
78             'replayFile',
79             array(
80                 'long_name'   => '--replay',
81                 'description' => "Replay messages from file instead from network",
82                 'help_name'   => 'FILE',
83                 'action'      => 'StoreString'
84             )
85         );
86
87         $p->addOption(
88             'debug',
89             array(
90                 'short_name'  => '-d',
91                 'long_name'   => '--debug',
92                 'description' => "Debug mode: Echo all received messages and events",
93                 'action'      => 'StoreTrue'
94             )
95         );
96         $p->addOption(
97             'debugEdss1',
98             array(
99                 'short_name'  => '-e',
100                 'long_name'   => '--debug-edss1',
101                 'description' => "Debug mode: Show EDSS1 messages only",
102                 'action'      => 'StoreTrue'
103             )
104         );
105
106         $this->cliParser = $p;
107     }
108
109     protected function fillConfig($config, $result)
110     {
111         $config->setIfNotEmpty('host', $result->options['host']);
112         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
113         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
114     }
115 }
116
117 ?>