CommandSerialiser::serialiseCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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