aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/CallMonitor.php
blob: 9abdba80c959cf61b8ab65c38f699ded6aa5389b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace callnotifier;

/**
 * Watches EDSS1 messages for calls. Keeps an internal call state
 * and notifies loggers of incoming and finished calls.
 *
 * Notifications:
 * - incomingCall
 * - finishedCall
 */
class CallMonitor
{
    protected $currentCalls = array();

    public function __construct($config, $log)
    {
        $this->config = $config;
        $this->log = $log;
    }

    public function handle(EDSS1_Message $msg)
    {
        $callId = $msg->callRef;
        if (!array_key_exists($callId, $this->currentCalls)) {
            $this->handleNew($callId, $msg);
        } else {
            $this->handleExisting($callId, $msg);
        }
    }

    protected function handleNew($callId, EDSS1_Message $msg)
    {
        if ($msg->type != EDSS1_Message::SETUP) {
            return;
        }
        $this->currentCalls[$callId] = new CallMonitor_Call();
        $this->handleSetup($callId, $msg);
    }


    protected function handleSetup($callId, EDSS1_Message $msg)
    {
        $call = $this->currentCalls[$callId];
        $call->start = time();
        foreach ($msg->parameters as $param) {
            switch ($param->type) {
            case EDSS1_Parameter::CALLING_PARTY_NUMBER:
                $call->from = $param->number;
                break;
            case EDSS1_Parameter::CALLED_PARTY_NUMBER:
                $call->to = $param->number;
                break;
            }
        }
    }


    protected function handleExisting($callId, EDSS1_Message $msg)
    {
        $call = $this->currentCalls[$callId];

        switch ($msg->type) {
        case EDSS1_Message::CALL_PROCEEDING:
            $this->log->log('incomingCall', array('call' => $call));
            break;

        case EDSS1_Message::RELEASE:
        case EDSS1_Message::RELEASE_COMPLETE:
            $call->end = time();
            $this->log->log('finishedCall', array('call' => $call));
            unset($this->currentCalls[$callId]);
            break;
        }
    }
}

?>