Completed
Pull Request — master (#27)
by Robin
03:22 queued 01:43
created

Client::help()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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']) {
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not seem to exist on object<Rvdv\Nntp\Command\AuthInfoCommand>.

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...
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']) {
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not seem to exist on object<Rvdv\Nntp\Command\AuthInfoCommand>.

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...
81 1
            throw new RuntimeException(sprintf('Could not authenticate with given username/password: %s', (string) $response));
82
        }
83
84 2
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (Rvdv\Nntp\Command\AuthInfoCommand) is incompatible with the return type declared by the interface Rvdv\Nntp\ClientInterface::authenticate of type Rvdv\Nntp\Response\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->authenticate($username, $password); (Rvdv\Nntp\Command\AuthInfoCommand) is incompatible with the return type declared by the interface Rvdv\Nntp\ClientInterface::connectAndAuthenticate of type Rvdv\Nntp\Response\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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
Bug introduced by
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