9abdba80c959cf61b8ab65c38f699ded6aa5389b
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Watches EDSS1 messages for calls. Keeps an internal call state
6  * and notifies loggers of incoming and finished calls.
7  *
8  * Notifications:
9  * - incomingCall
10  * - finishedCall
11  */
12 class CallMonitor
13 {
14     protected $currentCalls = array();
15
16     public function __construct($config, $log)
17     {
18         $this->config = $config;
19         $this->log = $log;
20     }
21
22     public function handle(EDSS1_Message $msg)
23     {
24         $callId = $msg->callRef;
25         if (!array_key_exists($callId, $this->currentCalls)) {
26             $this->handleNew($callId, $msg);
27         } else {
28             $this->handleExisting($callId, $msg);
29         }
30     }
31
32     protected function handleNew($callId, EDSS1_Message $msg)
33     {
34         if ($msg->type != EDSS1_Message::SETUP) {
35             return;
36         }
37         $this->currentCalls[$callId] = new CallMonitor_Call();
38         $this->handleSetup($callId, $msg);
39     }
40
41
42     protected function handleSetup($callId, EDSS1_Message $msg)
43     {
44         $call = $this->currentCalls[$callId];
45         $call->start = time();
46         foreach ($msg->parameters as $param) {
47             switch ($param->type) {
48             case EDSS1_Parameter::CALLING_PARTY_NUMBER:
49                 $call->from = $param->number;
50                 break;
51             case EDSS1_Parameter::CALLED_PARTY_NUMBER:
52                 $call->to = $param->number;
53                 break;
54             }
55         }
56     }
57
58
59     protected function handleExisting($callId, EDSS1_Message $msg)
60     {
61         $call = $this->currentCalls[$callId];
62
63         switch ($msg->type) {
64         case EDSS1_Message::CALL_PROCEEDING:
65             $this->log->log('incomingCall', array('call' => $call));
66             break;
67
68         case EDSS1_Message::RELEASE:
69         case EDSS1_Message::RELEASE_COMPLETE:
70             $call->end = time();
71             $this->log->log('finishedCall', array('call' => $call));
72             unset($this->currentCalls[$callId]);
73             break;
74         }
75     }
76 }
77
78 ?>