Completed
Push — master ( 8184b0...a4b4a5 )
by Douglas
02:13
created

PlaceRobot::setDirection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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