1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshCentrifugoBundle. |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\CentrifugoBundle\Model; |
14
|
|
|
|
15
|
|
|
use Fresh\CentrifugoBundle\Exception\UnexpectedValueException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* BatchRequest. |
19
|
|
|
* |
20
|
|
|
* @author Artem Henvald <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class BatchRequest implements CommandInterface |
23
|
|
|
{ |
24
|
|
|
/** @var string[] */ |
25
|
|
|
private $channels = []; |
26
|
|
|
|
27
|
|
|
/** @var CommandInterface[] */ |
28
|
|
|
private $commands = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param CommandInterface[] $commands |
32
|
|
|
* |
33
|
|
|
* @throws UnexpectedValueException |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $commands = []) |
36
|
|
|
{ |
37
|
|
|
foreach ($commands as $command) { |
38
|
|
|
if (!$command instanceof CommandInterface) { |
39
|
|
|
throw new UnexpectedValueException(\sprintf('Invalid command for batch request. Only instances of %s are allowed.', CommandInterface::class)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->addCommand($command); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param CommandInterface $command |
48
|
|
|
*/ |
49
|
|
|
public function addCommand(CommandInterface $command): void |
50
|
|
|
{ |
51
|
|
|
$this->commands[] = $command; |
52
|
|
|
$this->channels = \array_merge($this->channels, (array) $command->getChannels()); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return CommandInterface[] |
57
|
|
|
*/ |
58
|
|
|
public function getCommands(): iterable |
59
|
|
|
{ |
60
|
|
|
foreach ($this->commands as $command) { |
61
|
|
|
yield $command; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getNumberOfCommands(): int |
69
|
|
|
{ |
70
|
|
|
return \count($this->commands); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getChannels(): iterable |
77
|
|
|
{ |
78
|
|
|
foreach ($this->channels as $channelName) { |
79
|
|
|
yield $channelName; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function prepareLineDelimitedJson(): string |
87
|
|
|
{ |
88
|
|
|
$serializedCommands = []; |
89
|
|
|
|
90
|
|
|
foreach ($this->getCommands() as $command) { |
91
|
|
|
$serializedCommands[] = \json_encode($command, \JSON_THROW_ON_ERROR); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!empty($serializedCommands)) { |
95
|
|
|
$json = \implode("\n", $serializedCommands); |
96
|
|
|
} else { |
97
|
|
|
$json = '{}'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $json; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..