Completed
Push — master ( af3c22...fbabbd )
by Douglas
01:45
created

Robot::moveWestward()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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\Domain\Robot;
12
13
use Assert\Assert;
14
use Assert\Assertion;
15
use Reith\ToyRobot\Domain\Space\SpaceInterface;
16
use Reith\ToyRobot\Domain\Robot\Exception\NotPlacedInSpaceException;
17
18
class Robot
19
{
20
    /**
21
     * Purchased by Rupert, then tanked :).
22
     *
23
     * @var SpaceInterface
24
     */
25
    private $mySpace;
26
27
    private $placement;
28
29
    private $facingDirection;
30
31 8
    private function __construct(
32
        SpaceInterface $space,
33
        Place $placement,
34
        Direction $facingDirection
35
    ) {
36 8
        $this->mySpace = $space;
37 8
        $this->placement = $placement;
38 8
        $this->facingDirection = $facingDirection;
39 8
    }
40
41
    /**
42
     * @param SpaceInterface $space
43
     * @param Place          $placement
44
     * @param string|null    $facingDirection
45
     *
46
     * @return Robot
47
     *
48
     * @throws Assert\AssertionFailedException
49
     */
50 9
    public static function create(
51
        SpaceInterface $space,
52
        Place $placement,
53
        ?string $facingDirection = 'E'
54
    ): Robot {
55 9
        return new static(
56 9
            $space,
57 9
            $placement,
58 9
            new Direction($facingDirection)
59
        );
60
    }
61
62
    public function move(): Robot
63
    {
64
        $this->validateCanMove();
65
66
        $this->placement = $this->mySpace->move(
67
            $this->placement,
68
            $this->direction->getDirectionAsVector()
0 ignored issues
show
Bug introduced by
The property direction does not seem to exist. Did you mean facingDirection?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
        );
70
71
        return $this;
72
    }
73
74
    public function left(): Robot
75
    {
76
        $this->direction->rotateLeft();
0 ignored issues
show
Bug introduced by
The property direction does not seem to exist. Did you mean facingDirection?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
77
78
        return $this;
79
    }
80
81
    public function right(): Robot
82
    {
83
        $this->direction->rotateRight();
0 ignored issues
show
Bug introduced by
The property direction does not seem to exist. Did you mean facingDirection?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
84
85
        return $this;
86
    }
87
88 2
    public function moveNorthward(): Robot
89
    {
90 2
        $this->validateCanMove();
91
92 2
        $this->placement = $this->mySpace->move(
93 2
            $this->placement,
94 2
            CompassMove::northward()
95
        );
96
97 2
        return $this;
98
    }
99
100 1
    public function moveEastward(): Robot
101
    {
102 1
        $this->validateCanMove();
103
104 1
        $this->placement = $this->mySpace->move(
105 1
            $this->placement,
106 1
            CompassMove::eastward()
107
        );
108
109 1
        return $this;
110
    }
111
112 2
    public function moveSouthward(): Robot
113
    {
114 2
        $this->validateCanMove();
115
116 2
        $this->placement = $this->mySpace->move(
117 2
            $this->placement,
118 2
            CompassMove::southward()
119
        );
120
121 1
        return $this;
122
    }
123
124 2
    public function moveWestward(): Robot
125
    {
126 2
        $this->validateCanMove();
127
128 2
        $this->placement = $this->mySpace->move(
129 2
            $this->placement,
130 2
            CompassMove::westward()
131
        );
132
133 1
        return $this;
134
    }
135
136 1
    public function getPlacement(): Place
137
    {
138 1
        return $this->placement;
139
    }
140
141
    /**
142
     * validateCanMove.
143
     *
144
     * The robot requires a space during construction, however,
145
     * this is extra precautionary in case the space is removed
146
     *
147
     * @throws NotPlacedInSpaceException
148
     */
149 4
    private function validateCanMove(): void
150
    {
151 4
        if (!$this->mySpace) {
152
            throw new NotPlacedInSpaceException(
153
                'I cannot move until placed in space'
154
            );
155
        }
156 4
    }
157
}
158