Completed
Push — master ( 37a451...0fec48 )
by Artem
01:36
created

Centrifugo   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 16
dl 0
loc 167
rs 10
c 0
b 0
f 0
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\Service;
14
15
use Fresh\CentrifugoBundle\Logger\CommandHistoryLogger;
16
use Fresh\CentrifugoBundle\Model;
17
use Fresh\CentrifugoBundle\Model\CommandInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Profiler\Profiler;
20
use Symfony\Contracts\HttpClient\HttpClientInterface;
21
22
/**
23
 * Centrifugo.
24
 *
25
 * @author Artem Henvald <[email protected]>
26
 */
27
class Centrifugo implements CentrifugoInterface
28
{
29
    private string $endpoint;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
30
    private string $apiKey;
31
    private HttpClientInterface $httpClient;
32
    private ResponseProcessor $responseProcessor;
33
    private CommandHistoryLogger $commandHistoryLogger;
34
    private CentrifugoChecker $centrifugoChecker;
35
    private bool $profilerEnabled;
36
37
    /**
38
     * @param string               $endpoint
39
     * @param string               $apiKey
40
     * @param HttpClientInterface  $httpClient
41
     * @param ResponseProcessor    $responseProcessor
42
     * @param CommandHistoryLogger $commandHistoryLogger
43
     * @param CentrifugoChecker    $centrifugoChecker
44
     * @param Profiler|null        $profiler
45
     */
46
    public function __construct(string $endpoint, string $apiKey, HttpClientInterface $httpClient, ResponseProcessor $responseProcessor, CommandHistoryLogger $commandHistoryLogger, CentrifugoChecker $centrifugoChecker, ?Profiler $profiler)
47
    {
48
        $this->endpoint = $endpoint;
49
        $this->apiKey = $apiKey;
50
        $this->httpClient = $httpClient;
51
        $this->responseProcessor = $responseProcessor;
52
        $this->commandHistoryLogger = $commandHistoryLogger;
53
        $this->centrifugoChecker = $centrifugoChecker;
54
        $this->profilerEnabled = $profiler instanceof Profiler;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function publish(array $data, string $channel): void
61
    {
62
        $this->doSendCommand(new Model\PublishCommand($data, $channel));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function broadcast(array $data, array $channels): void
69
    {
70
        $this->doSendCommand(new Model\BroadcastCommand($data, $channels));
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function unsubscribe(string $user, string $channel): void
77
    {
78
        $this->doSendCommand(new Model\UnsubscribeCommand($user, $channel));
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function disconnect(string $user): void
85
    {
86
        $this->doSendCommand(new Model\DisconnectCommand($user));
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function presence(string $channel): array
93
    {
94
        return (array) $this->doSendCommand(new Model\PresenceCommand($channel));
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function presenceStats(string $channel): array
101
    {
102
        return (array) $this->doSendCommand(new Model\PresenceStatsCommand($channel));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function history(string $channel): array
109
    {
110
        return (array) $this->doSendCommand(new Model\HistoryCommand($channel));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function historyRemove(string $channel): void
117
    {
118
        $this->doSendCommand(new Model\HistoryRemoveCommand($channel));
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function channels(): array
125
    {
126
        return (array) $this->doSendCommand(new Model\ChannelsCommand());
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function info(): array
133
    {
134
        return (array) $this->doSendCommand(new Model\InfoCommand());
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function batchRequest(array $commands): array
141
    {
142
        return (array) $this->doSendCommand(new Model\BatchRequest($commands));
143
    }
144
145
    /**
146
     * @param CommandInterface $command
147
     *
148
     * @return array|null
149
     */
150
    private function doSendCommand(CommandInterface $command): ?array
151
    {
152
        foreach ($command->getChannels() as $channel) {
153
            $this->centrifugoChecker->assertValidChannelName($channel);
154
        }
155
156
        if ($command instanceof Model\BatchRequest) {
157
            $json = $command->prepareLineDelimitedJson();
158
        } else {
159
            $json = \json_encode($command, \JSON_THROW_ON_ERROR);
160
        }
161
162
        if ($this->profilerEnabled) {
163
            $this->commandHistoryLogger->increaseRequestsCount();
164
        }
165
166
        $response = $this->httpClient->request(
167
            Request::METHOD_POST,
168
            $this->endpoint,
169
            [
170
                'headers' => [
171
                    'Authorization' => 'apikey '.$this->apiKey,
172
                    'Content-Type' => 'application/json',
173
                ],
174
                'body' => $json,
175
            ]
176
        );
177
178
        return $this->responseProcessor->processResponse($command, $response);
179
    }
180
}
181