Completed
Push — master ( 9bb610...8aa334 )
by Robin
01:44
created

src/Client.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the NNTP library.
5
 *
6
 * (c) Robin van der Vleuten <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rvdv\Nntp;
13
14
use Rvdv\Nntp\Command\CommandInterface;
15
use Rvdv\Nntp\Connection\ConnectionInterface;
16
use Rvdv\Nntp\Exception\RuntimeException;
17
use Rvdv\Nntp\Response\Response;
18
19
/**
20
 * @author Robin van der Vleuten <[email protected]>
21
 */
22
class Client implements ClientInterface
23
{
24
    /**
25
     * @var ConnectionInterface
26
     */
27
    private $connection;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param ConnectionInterface $connection
33
     */
34 8
    public function __construct(ConnectionInterface $connection)
35
    {
36 8
        $this->connection = $connection;
37 8
    }
38
39
    /**
40
     * Get the connection instance.
41
     *
42
     * @return ConnectionInterface
43
     */
44 1
    public function getConnection()
45
    {
46 1
        return $this->connection;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function connect()
53
    {
54 1
        return $this->connection->connect();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function disconnect()
61
    {
62 1
        $this->connection->disconnect();
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 4
    public function authenticate($username, $password = null)
69
    {
70 4
        $response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_USER, (string) $username);
71
72 4
        if ($response->getStatusCode() === Response::$codes['PasswordRequired']) {
73 2
            if (null === $password) {
74 1
                throw new RuntimeException('NNTP server asks for further authentication but no password is given');
75
            }
76
77 1
            $response = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_PASS, (string) $password);
78 1
        }
79
80 3
        if ($response->getStatusCode() !== Response::$codes['AuthenticationAccepted']) {
81 1
            throw new RuntimeException(sprintf('Could not authenticate with given username/password: %s', (string) $response));
82
        }
83
84 2
        return $response;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function connectAndAuthenticate($username = null, $password = null)
91
    {
92
        $response = $this->connect();
93
94
        if (!in_array($response->getStatusCode(), [Response::$codes['PostingAllowed'], Response::$code['PostingProhibited']])) {
95
            throw new RuntimeException(sprintf('Unsuccessful response received: %s', (string) $response));
96
        }
97
98
        if ($username === null) {
99
            return $response;
100
        }
101
102
        return $this->authenticate($username, $password);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 5
    public function authInfo($type, $value)
109
    {
110 5
        return $this->sendCommand(new Command\AuthInfoCommand($type, $value));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function body($article)
117
    {
118
        return $this->sendCommand(new Command\BodyCommand($article));
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function listGroups($keyword = null, $arguments = null)
125
    {
126
        return $this->sendCommand(new Command\ListCommand($keyword, $arguments));
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public function group($name)
133
    {
134 1
        return $this->sendCommand(new Command\GroupCommand($name));
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1
    public function help()
141
    {
142 1
        return $this->sendCommand(new Command\HelpCommand());
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 1
    public function overviewFormat()
149
    {
150 1
        return $this->sendCommand(new Command\OverviewFormatCommand());
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function post($groups, $subject, $body, $from, $headers = null)
157
    {
158
        $command = $this->sendCommand(new Command\PostCommand());
159
        $response = $command->getResponse();
160
161
        if ($response->getStatusCode() === Response::$codes['SendArticle']) {
162
            $command = $this->postArticle($groups, $subject, $body, $from, $headers);
163
            $response = $command->getResponse();
0 ignored issues
show
The method getResponse() does not seem to exist on object<Rvdv\Nntp\Response\ResponseInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
        }
165
166
        if ($response->getStatusCode() !== Response::$codes['ArticleReceived']) {
167
            throw new RuntimeException(sprintf('Posting failed: %s', (string) $response));
168
        }
169
170
        return $response;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function postArticle($groups, $subject, $body, $from, $headers = null)
177
    {
178
        return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers));
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 1
    public function quit()
185
    {
186 1
        return $this->sendCommand(new Command\QuitCommand());
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 1
    public function xfeature($feature)
193
    {
194 1
        return $this->sendCommand(new Command\XFeatureCommand($feature));
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 1
    public function xover($from, $to, array $format)
201
    {
202 1
        return $this->sendCommand(new Command\XoverCommand($from, $to, $format));
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208 1
    public function xzver($from, $to, array $format)
209
    {
210 1
        return $this->sendCommand(new Command\XzverCommand($from, $to, $format));
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 5
    public function sendCommand(CommandInterface $command)
217
    {
218 5
        return $this->connection->sendCommand($command);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function sendArticle(CommandInterface $command)
225
    {
226
        return $this->connection->sendArticle($command);
227
    }
228
}
229