|
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\CommandHandler; |
|
12
|
|
|
|
|
13
|
|
|
use MathPHP\LinearAlgebra\Vector; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Reith\ToyRobot\Domain\Robot\RobotRepositoryInterface; |
|
16
|
|
|
use Reith\ToyRobot\Domain\Robot\Robot; |
|
17
|
|
|
|
|
18
|
|
|
use Reith\ToyRobot\Messaging\Annotation\Subscribe; |
|
19
|
|
|
use Reith\ToyRobot\Messaging\Command\TurnLeft; |
|
20
|
|
|
use Reith\ToyRobot\Messaging\Command\TurnRight; |
|
21
|
|
|
use Reith\ToyRobot\Messaging\Command\MoveForward; |
|
22
|
|
|
|
|
23
|
|
|
final class RobotMover |
|
24
|
|
|
{ |
|
25
|
|
|
private $repository; |
|
26
|
|
|
|
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param RobotRepositoryInterface $repository |
|
31
|
|
|
* @param LoggerInterface $logger |
|
32
|
|
|
*/ |
|
33
|
7 |
|
public function __construct( |
|
34
|
|
|
RobotRepositoryInterface $repository, |
|
35
|
|
|
LoggerInterface $logger |
|
36
|
|
|
) { |
|
37
|
7 |
|
$this->repository = $repository; |
|
38
|
7 |
|
$this->logger = $logger; |
|
39
|
7 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @Subscribe |
|
43
|
|
|
* |
|
44
|
|
|
* @param TurnLeft $command |
|
45
|
|
|
*/ |
|
46
|
1 |
View Code Duplication |
public function handleTurningLeft(TurnLeft $command): void |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
1 |
|
$robot = $this->getRobot(); |
|
49
|
|
|
|
|
50
|
1 |
|
$robot->left(); |
|
51
|
|
|
|
|
52
|
1 |
|
$this->repository->save($robot); |
|
53
|
|
|
|
|
54
|
1 |
|
$this->logger->info('Robot turned left'); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @Subscribe |
|
59
|
|
|
* |
|
60
|
|
|
* @param TurnRight $command |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function handleTurningRight(TurnRight $command): void |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
1 |
|
$robot = $this->getRobot(); |
|
65
|
|
|
|
|
66
|
1 |
|
$robot->right(); |
|
67
|
|
|
|
|
68
|
1 |
|
$this->repository->save($robot); |
|
69
|
|
|
|
|
70
|
1 |
|
$this->logger->info('Robot turned right'); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @Subscribe |
|
75
|
|
|
* |
|
76
|
|
|
* @param MoveForward $command |
|
77
|
|
|
*/ |
|
78
|
1 |
View Code Duplication |
public function handleMoveForward(MoveForward $command): void |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
1 |
|
$robot = $this->getRobot(); |
|
81
|
|
|
|
|
82
|
1 |
|
$robot->move(); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->repository->save($robot); |
|
85
|
|
|
|
|
86
|
1 |
|
$this->logger->info('Robot moved forward'); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return Robot |
|
91
|
|
|
*/ |
|
92
|
3 |
|
private function getRobot(): Robot |
|
93
|
|
|
{ |
|
94
|
3 |
|
$robot = $this->repository->load(); |
|
95
|
|
|
|
|
96
|
3 |
|
if (!($robot instanceof Robot)) { |
|
97
|
|
|
throw new \RuntimeException( |
|
98
|
|
|
'Robot cannot be found. Please PLACE the robot first.' |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
return $robot; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.