1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Beanie\Tube; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Beanie\Beanie; |
8
|
|
|
use Beanie\Command\CommandFactory; |
9
|
|
|
use Beanie\Command\CommandInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TubeStatus |
13
|
|
|
* |
14
|
|
|
* Keeps track of the currently used and watched tubes. |
15
|
|
|
* |
16
|
|
|
* @package Beanie\Server |
17
|
|
|
*/ |
18
|
|
|
class TubeStatus |
19
|
|
|
{ |
20
|
|
|
const TRANSFORM_USE = 1; |
21
|
|
|
const TRANSFORM_WATCHED = 2; |
22
|
|
|
const TRANSFORM_BOTH = 3; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $currentTube = Beanie::DEFAULT_TUBE; |
26
|
|
|
|
27
|
|
|
/** @var string[] */ |
28
|
|
|
protected $watchedTubes = [Beanie::DEFAULT_TUBE]; |
29
|
|
|
|
30
|
|
|
/** @var CommandFactory */ |
31
|
|
|
protected $commandFactory; |
32
|
|
|
|
33
|
77 |
|
public function __construct() |
34
|
|
|
{ |
35
|
77 |
|
$this->commandFactory = CommandFactory::instance(); |
36
|
77 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $tubeName |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
31 |
|
public function setCurrentTube($tubeName) |
43
|
|
|
{ |
44
|
31 |
|
$this->currentTube = $tubeName; |
45
|
31 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
21 |
|
public function getCurrentTube() |
52
|
|
|
{ |
53
|
21 |
|
return $this->currentTube; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $tubeName |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
10 |
|
public function addWatchedTube($tubeName) |
61
|
|
|
{ |
62
|
10 |
|
if (!array_search($tubeName, $this->watchedTubes)) { |
63
|
10 |
|
$this->watchedTubes[] = $tubeName; |
64
|
10 |
|
} |
65
|
|
|
|
66
|
10 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $tubeName |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
4 |
|
public function removeWatchedTube($tubeName) |
74
|
|
|
{ |
75
|
4 |
|
if (($position = array_search($tubeName, $this->watchedTubes)) !== false) { |
76
|
3 |
|
array_splice($this->watchedTubes, $position, 1); |
77
|
3 |
|
} |
78
|
4 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \string[] |
83
|
|
|
*/ |
84
|
22 |
|
public function getWatchedTubes() |
85
|
|
|
{ |
86
|
22 |
|
return $this->watchedTubes; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string[] $tubes |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
15 |
|
public function setWatchedTubes(array $tubes) |
94
|
|
|
{ |
95
|
15 |
|
$this->watchedTubes = $tubes; |
96
|
15 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param TubeStatus $goal |
101
|
|
|
* @param int $mode |
102
|
|
|
* @return \Beanie\Command\CommandInterface[] |
103
|
|
|
*/ |
104
|
5 |
|
public function transformTo(TubeStatus $goal, $mode = self::TRANSFORM_BOTH) |
105
|
|
|
{ |
106
|
5 |
|
$commands = $this->calculateTransformationTo($goal, $mode); |
107
|
|
|
|
108
|
5 |
|
if ($mode & self::TRANSFORM_USE) { |
109
|
3 |
|
$this->setCurrentTube($goal->getCurrentTube()); |
110
|
3 |
|
} |
111
|
|
|
|
112
|
5 |
|
if ($mode & self::TRANSFORM_WATCHED) { |
113
|
4 |
|
$this->setWatchedTubes($goal->getWatchedTubes()); |
114
|
4 |
|
} |
115
|
|
|
|
116
|
5 |
|
return $commands; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param TubeStatus $goal |
121
|
|
|
* @param int $mode |
122
|
|
|
* @return \Beanie\Command\CommandInterface[] |
123
|
|
|
*/ |
124
|
15 |
|
public function calculateTransformationTo(TubeStatus $goal, $mode = self::TRANSFORM_BOTH) |
125
|
|
|
{ |
126
|
15 |
|
$commands = []; |
127
|
|
|
|
128
|
15 |
|
if ($mode & self::TRANSFORM_WATCHED) { |
129
|
12 |
|
$commands = $this->calculateTransformWatched($goal->getWatchedTubes()); |
130
|
12 |
|
} |
131
|
|
|
|
132
|
|
|
if ( |
133
|
15 |
|
$mode & self::TRANSFORM_USE && |
134
|
11 |
|
$goal->getCurrentTube() !== $this->currentTube |
135
|
15 |
|
) { |
136
|
6 |
|
$commands[] = $this->commandFactory->create(CommandInterface::COMMAND_USE, [$goal->getCurrentTube()]); |
137
|
6 |
|
} |
138
|
|
|
|
139
|
15 |
|
return $commands; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string[] $otherWatchedTubes |
144
|
|
|
* @return \Beanie\Command\CommandInterface[] |
145
|
|
|
*/ |
146
|
12 |
|
protected function calculateTransformWatched(array $otherWatchedTubes = []) |
147
|
|
|
{ |
148
|
12 |
|
return array_merge( |
149
|
12 |
|
$this->calculateDiffTubes($otherWatchedTubes, $this->getWatchedTubes(), CommandInterface::COMMAND_WATCH), |
150
|
12 |
|
$this->calculateDiffTubes($this->getWatchedTubes(), $otherWatchedTubes, CommandInterface::COMMAND_IGNORE) |
151
|
12 |
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string[] $tubes |
156
|
|
|
* @param string[] $otherTubes |
157
|
|
|
* @param string $command |
158
|
|
|
* @return CommandInterface[] |
159
|
|
|
* @throws \Beanie\Exception\InvalidArgumentException |
160
|
|
|
*/ |
161
|
12 |
|
protected function calculateDiffTubes($tubes, $otherTubes, $command) |
162
|
|
|
{ |
163
|
12 |
|
$commands = []; |
164
|
|
|
|
165
|
12 |
|
foreach (array_diff($tubes, $otherTubes) as $tube) { |
166
|
8 |
|
$commands[] = $this->commandFactory->create($command, [$tube]); |
167
|
12 |
|
} |
168
|
|
|
|
169
|
12 |
|
return $commands; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|