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\Exception\CentrifugoException; |
16
|
|
|
use Fresh\CentrifugoBundle\Exception\InvalidArgumentException; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CentrifugoChecker. |
22
|
|
|
* |
23
|
|
|
* @author Artem Henvald <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class CentrifugoChecker |
26
|
|
|
{ |
27
|
|
|
/** @var int */ |
28
|
|
|
private $channelMaxLength; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param int $centrifugoChannelMaxLength |
32
|
|
|
*/ |
33
|
|
|
public function __construct(int $centrifugoChannelMaxLength) |
34
|
|
|
{ |
35
|
|
|
$this->channelMaxLength = $centrifugoChannelMaxLength; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $channelName |
40
|
|
|
* |
41
|
|
|
* @throws InvalidArgumentException |
42
|
|
|
*/ |
43
|
|
|
public function assertValidChannelName(string $channelName): void |
44
|
|
|
{ |
45
|
|
|
if (false === \mb_detect_encoding($channelName, 'ASCII', true)) { |
46
|
|
|
throw new InvalidArgumentException('Invalid channel name. Only ASCII symbols must be used in channel string.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (\strlen($channelName) > $this->channelMaxLength) { |
50
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid channel name length. Maximum allowed length is %d.', $this->channelMaxLength)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ResponseInterface $response |
56
|
|
|
* |
57
|
|
|
* @throws CentrifugoException |
58
|
|
|
*/ |
59
|
|
|
public function assertValidResponseStatusCode(ResponseInterface $response): void |
60
|
|
|
{ |
61
|
|
|
if (Response::HTTP_OK !== $response->getStatusCode()) { |
62
|
|
|
throw new CentrifugoException('Wrong status code for Centrifugo response'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ResponseInterface $response |
68
|
|
|
* |
69
|
|
|
* @throws CentrifugoException |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function assertValidResponseHeaders(ResponseInterface $response): void |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$headers = $response->getHeaders(false); |
74
|
|
|
|
75
|
|
|
if (!isset($headers['content-type'])) { |
76
|
|
|
throw new CentrifugoException('Missing "content-type" header in Centrifugo response'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ResponseInterface $response |
82
|
|
|
* |
83
|
|
|
* @throws CentrifugoException |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function assertValidResponseContentType(ResponseInterface $response): void |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$headers = $response->getHeaders(false); |
88
|
|
|
|
89
|
|
|
if (!\in_array('application/json', $headers['content-type'], true)) { |
90
|
|
|
throw new CentrifugoException('Unexpected content type for Centrifugo response'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.