1 | <?php |
||
27 | class Connection implements ConnectionInterface |
||
28 | { |
||
29 | const BUFFER_SIZE = 1024; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $host; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $port; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $secure; |
||
45 | |||
46 | /** |
||
47 | * @var SocketInterface |
||
48 | */ |
||
49 | private $socket; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param string $host the host of the NNTP server |
||
55 | * @param int $port the port of the NNTP server |
||
56 | * @param bool $secure a bool indicating if a secure connection should be established |
||
57 | * @param SocketInterface $socket an optional socket wrapper instance |
||
58 | */ |
||
59 | 7 | public function __construct($host, $port, $secure = false, SocketInterface $socket = null) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 3 | public function connect() |
|
71 | { |
||
72 | 3 | $this->socket->connect(sprintf('tcp://%s:%d', $this->host, $this->port)); |
|
73 | |||
74 | 2 | if ($this->secure) { |
|
75 | 1 | $this->socket->enableCrypto(true); |
|
76 | 1 | } |
|
77 | |||
78 | 2 | return $this->getResponse(); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 1 | public function disconnect() |
|
85 | { |
||
86 | 1 | $this->socket->disconnect(); |
|
87 | 1 | } |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 3 | public function sendCommand(CommandInterface $command) |
|
93 | { |
||
94 | 3 | $commandString = $command(); |
|
95 | |||
96 | // NNTP/RFC977 only allows command up to 512 (-2 \r\n) chars. |
||
97 | 3 | if (strlen($commandString) > 510) { |
|
98 | 1 | throw new InvalidArgumentException('Failed to write to socket: command exceeded 510 characters'); |
|
99 | } |
||
100 | |||
101 | 2 | if (strlen($commandString."\r\n") !== $this->socket->write($commandString."\r\n")) { |
|
102 | 1 | throw new RuntimeException('Failed to write to socket'); |
|
103 | } |
||
104 | |||
105 | 1 | $response = $this->getResponse(); |
|
106 | |||
107 | 1 | if ($command->isMultiLine() && ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399)) { |
|
108 | $response = $command->isCompressed() ? $this->getCompressedResponse($response) : $this->getMultiLineResponse($response); |
||
109 | } |
||
110 | |||
111 | 1 | return $this->callCommandHandlerForResponse($command, $response); |
|
|
|||
112 | } |
||
113 | |||
114 | public function sendArticle(CommandInterface $command) |
||
126 | |||
127 | 3 | private function getResponse() |
|
128 | { |
||
129 | 3 | $buffer = ''; |
|
147 | |||
148 | private function getMultiLineResponse(Response $response) |
||
175 | |||
176 | private function getCompressedResponse(Response $response) |
||
216 | |||
217 | 1 | private function callCommandHandlerForResponse(CommandInterface $command, ResponseInterface $response) |
|
240 | } |
||
241 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.