Pool::prepareEnv()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 4
Ratio 40 %

Importance

Changes 0
Metric Value
cc 2
dl 4
loc 10
rs 9.9332
c 0
b 0
f 0
nc 2
nop 1
1
<?php
2
namespace PHPDaemon\Clients\Asterisk;
3
4
use PHPDaemon\Network\Client;
5
6
/**
7
 * Class Pool
8
 * @package PHPDaemon\Clients\Asterisk
9
 */
10
class Pool extends Client
11
{
12
13
    /**
14
     * @var array Beginning of the string in the header or value that indicates whether the save value case
15
     */
16
    public static $safeCaseValues = ['dialstring', 'callerid', 'connectedline'];
17
    /**
18
     * @var array Asterisk Call Manager Interface versions for each session
19
     */
20
    protected $amiVersions = [];
21
22
    /**
23
     * Prepares environment scope
24
     * @param  string $data Address
25
     * @return array
26
     */
27
    public static function prepareEnv($data)
28
    {
29
        $result = [];
30
        $rows = explode("\n", $data);
31 View Code Duplication
        for ($i = 0, $s = sizeof($rows); $i < $s; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            $e = self::extract($rows[$i]);
33
            $result[$e[0]] = $e[1];
34
        }
35
        return $result;
36
    }
37
38
    /**
39
     * Extract key and value pair from line.
40
     * @param  string $line
41
     * @return array
42
     */
43
    public static function extract($line)
44
    {
45
        $e = explode(': ', $line, 2);
46
        $header = strtolower(trim($e[0]));
47
        $value = isset($e[1]) ? trim($e[1]) : null;
48
        $safe = false;
49
50
        foreach (self::$safeCaseValues as $item) {
51
            if (strncasecmp($header, $item, mb_orig_strlen($item)) === 0) {
52
                $safe = true;
53
                break;
54
            }
55
            if (strncasecmp($value, $item, mb_orig_strlen($item)) === 0) {
56
                $safe = true;
57
                break;
58
            }
59
        }
60
61
        if (!$safe) {
62
            $value = strtolower($value);
63
        }
64
65
        return [$header, $value];
66
    }
67
68
    /**
69
     * Sets AMI version
70
     * @param  string $addr Address
71
     * @param  string $ver Version
72
     * @return void
73
     */
74
    public function setAmiVersion($addr, $ver)
75
    {
76
        $this->amiVersions[$addr] = $ver;
77
    }
78
79
    /**
80
     * Setting default config options
81
     * Overriden from NetworkClient::getConfigDefaults
82
     * @return array
83
     */
84
    protected function getConfigDefaults()
85
    {
86
        return [
87
            // [string] Auth hash type
88
            'authtype' => 'md5',
89
            // [integer] Port
90
            'port' => 5280,
91
        ];
92
    }
93
}
94