CommandSerialiser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 51
ccs 17
cts 17
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A factory() 0 4 1
A serialiseArguments() 0 10 2
A serialiseCommand() 0 6 1
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient;
14
15
use Graze\UnicontrollerClient\StringEscaper;
16
17
class CommandSerialiser
18
{
19
    /**
20
     * @var StringEscaper
21
     */
22
    private $stringEscaper;
23
24
    /**
25
     * @param StringEscaper $stringEscaper
26
     */
27 21
    public function __construct(StringEscaper $stringEscaper)
28
    {
29 21
        $this->stringEscaper = $stringEscaper;
30 21
    }
31
    /**
32
     * @param string $command
33
     * @param string $argumentsSerialised
34
     * @return string
35
     */
36 1
    public function serialiseCommand($command, $argumentsSerialised)
37
    {
38 1
        return sprintf(
39 1
            "\x01%s=%s\x17\r\n",
40 1
            $command,
41
            $argumentsSerialised
42 1
        );
43
    }
44
45
    /**
46
     * @param array $arguments
47
     * @return string
48
     */
49
    public function serialiseArguments(array $arguments)
50
    {
51
        // escape strings
52 1
        array_walk($arguments, function (&$value) {
53 1
            if (!is_numeric($value)) {
54 1
                $value = $this->stringEscaper->escape($value);
55 1
            }
56 1
        });
57
58 1
        return implode(',', $arguments);
59
    }
60
61
    /**
62
     * @return CommandSerialiser
63
     */
64 19
    public static function factory()
65
    {
66 19
        return new static(
67 19
            new StringEscaper()
68 19
        );
69
    }
70
}
71