PlaceRobot   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setCoordinates() 0 13 1
A setDirection() 0 8 1
A getCoordinates() 0 4 1
A getDirection() 0 4 1
1
<?php
2
3
/**
4
 * (c) 2018 Douglas Reith.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace Reith\ToyRobot\Messaging\Command;
12
13
use Assert\Assertion;
14
15
class PlaceRobot
16
{
17
    private $coordinates;
18
19
    private $direction;
20
21
    /**
22
     * @param array  $coordinates
23
     * @param string $direction
24
     */
25 8
    public function __construct(array $coordinates, string $direction)
26
    {
27 8
        $this->setCoordinates($coordinates);
28 8
        $this->setDirection($direction);
29 8
    }
30
31
    /**
32
     * @param array $coordinates
33
     */
34 8
    private function setCoordinates(array $coordinates): void
35
    {
36
        // Sanitize command input, ensure
37
        // int[]
38 8
        $this->coordinates = array_map(
39
            function ($coord) {
40 8
                Assertion::numeric($coord);
41
42 8
                return (int) $coord;
43 8
            },
44 8
            $coordinates
45
        );
46 8
    }
47
48
    /**
49
     * @param string $direction
50
     */
51 8
    private function setDirection(string $direction): void
52
    {
53
        // Sanitize direction, 1 char capital
54 8
        $direction = strtoupper(trim($direction));
55 8
        Assertion::length($direction, 1);
56 8
        Assertion::choice($direction, ['N', 'E', 'S', 'W']);
57 8
        $this->direction = $direction;
58 8
    }
59
60 6
    public function getCoordinates(): array
61
    {
62 6
        return $this->coordinates;
63
    }
64
65 6
    public function getDirection(): string
66
    {
67 6
        return $this->direction;
68
    }
69
}
70