928e4ce929cac281f34ac9118a2efd87609f6ed1
[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         $handler = new MessageHandler($this->config);
27         if ($this->config->replayFile !== null) {
28             $sourceClass = 'callnotifier\Source_File';
29         } else {
30             $sourceClass = 'callnotifier\Source_Remote';
31         }
32         $source = new $sourceClass($this->config, $handler);
33         $source->run();
34     }
35
36     public function setupCli()
37     {
38         $p = new \Console_CommandLine();
39         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
40         $p->version = '0.0.1';
41
42         $p->addOption(
43             'host',
44             array(
45                 'short_name'  => '-h',
46                 'long_name'   => '--host',
47                 'description' => 'IP of COMpact 3000',
48                 'help_name'   => 'IP',
49                 'action'      => 'StoreString'
50             )
51         );
52
53         $p->addOption(
54             'dumpFile',
55             array(
56                 'long_name'   => '--dump',
57                 'description' => 'dump messages into file for later replay',
58                 'help_name'   => 'FILE',
59                 'action'      => 'StoreString'
60             )
61         );
62         $p->addOption(
63             'replayFile',
64             array(
65                 'long_name'   => '--replay',
66                 'description' => "Replay messages from file instead from network",
67                 'help_name'   => 'FILE',
68                 'action'      => 'StoreString'
69             )
70         );
71
72         $this->cliParser = $p;
73     }
74
75     protected function fillConfig($config, $result)
76     {
77         $config->setIfNotEmpty('host', $result->options['host']);
78         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
79         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
80     }
81 }
82
83 ?>