Completed
Push — master ( 7d6afc...af3c22 )
by Douglas
02:06
created

PlaceRobot   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
ccs 20
cts 22
cp 0.9091
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 4
    public function __construct(array $coordinates, string $direction)
26
    {
27 4
        $this->setCoordinates($coordinates);
28 4
        $this->setDirection($direction);
29 4
    }
30
31
    /**
32
     * @param array $coordinates
33
     */
34 4
    private function setCoordinates(array $coordinates): void
35
    {
36
        // Sanitize command input, ensure
37
        // int[]
38 4
        $this->coordinates = array_map(
39 4
            function ($coord) {
40 4
                Assertion::numeric($coord);
41
42 4
                return (int) $coord;
43 4
            },
44 4
            $coordinates
45
        );
46 4
    }
47
48
    /**
49
     * @param string $direction
50
     */
51 4
    private function setDirection(string $direction): void
52
    {
53
        // Sanitize direction, 1 char capital
54 4
        $direction = strtoupper(trim($direction));
55 4
        Assertion::length($direction, 1);
56 4
        Assertion::choice($direction, ['N', 'E', 'S', 'W']);
57 4
        $this->direction = $direction;
58 4
    }
59
60 2
    public function getCoordinates(): array
61
    {
62 2
        return $this->coordinates;
63
    }
64
65
    public function getDirection(): string
66
    {
67
        return $this->direction;
68
    }
69
}
70