Completed
Push — master ( 83696f...79674b )
by Robin
8s
created

Client::listGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
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 9
    public function __construct(ConnectionInterface $connection)
35
    {
36 9
        $this->connection = $connection;
37 9
    }
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 2
    public function disconnect()
61
    {
62 2
        $this->connection->disconnect();
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 4
    public function authenticate($username, $password = null)
69
    {
70 4
        $command = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_USER, (string) $username);
71 4
        $response = $command->getResponse();
72
73 4
        if ($response->getStatusCode() === Response::PASSWORD_REQUIRED) {
74 2
            if (null === $password) {
75 1
                throw new RuntimeException('NNTP server asks for further authentication but no password is given');
76
            }
77
78 1
            $command = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_PASS, (string) $password);
79 1
            $response = $command->getResponse();
80 1
        }
81
82 3
        if ($response->getStatusCode() !== Response::AUTHENTICATION_ACCEPTED) {
83 1
            throw new RuntimeException(sprintf('Could not authenticate with given username/password: %s', (string) $response));
84
        }
85
86 2
        return $response;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function connectAndAuthenticate($username = null, $password = null)
93
    {
94
        $response = $this->connect();
95
96
        if (!in_array($response->getStatusCode(), [Response::POSTING_ALLOWED, RESPONSE::POSTING_PROHIBITED])) {
97
            throw new RuntimeException(sprintf('Unsuccessful response received: %s', (string) $response));
98
        }
99
100
        if ($username !== null) {
101
            return $this->authenticate($username, $password);
102
        }
103
104
        return $response;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 5
    public function authInfo($type, $value)
111
    {
112 5
        return $this->sendCommand(new Command\AuthInfoCommand($type, $value));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function body($article)
119
    {
120
        return $this->sendCommand(new Command\BodyCommand($article));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function listGroups($keyword = null, $arguments = null)
127
    {
128
        return $this->sendCommand(new Command\ListCommand($keyword, $arguments));
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function group($name)
135
    {
136 1
        return $this->sendCommand(new Command\GroupCommand($name));
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function help()
143
    {
144 1
        return $this->sendCommand(new Command\HelpCommand());
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1
    public function overviewFormat()
151
    {
152 1
        return $this->sendCommand(new Command\OverviewFormatCommand());
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function post($groups, $subject, $body, $from, $headers = null)
159
    {
160
        $command = $this->sendCommand(new Command\PostCommand());
161
        $response = $command->getResponse();
162
163
        if ($response->getStatusCode() === Response::SEND_ARTICLE) {
164
            $command = $this->postArticle($groups, $subject, $body, $from, $headers);
165
            $response = $command->getResponse();
166
        }
167
168
        if ($response->getStatusCode() !== Response::ARTICLE_RECEIVED) {
169
            throw new RuntimeException(sprintf('Posting failed: %s', (string) $response));
170
        }
171
172
        return $response;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function postArticle($groups, $subject, $body, $from, $headers = null)
179
    {
180
        return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->sendArticl...ody, $from, $headers)); (Rvdv\Nntp\Command\CommandInterface) is incompatible with the return type declared by the interface Rvdv\Nntp\ClientInterface::postArticle 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...
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 1
    public function quit()
187
    {
188 1
        return $this->sendCommand(new Command\QuitCommand());
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194 1
    public function xfeature($feature)
195
    {
196 1
        return $this->sendCommand(new Command\XFeatureCommand($feature));
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 1
    public function xover($from, $to, array $format)
203
    {
204 1
        return $this->sendCommand(new Command\XoverCommand($from, $to, $format));
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 1
    public function xzver($from, $to, array $format)
211
    {
212 1
        return $this->sendCommand(new Command\XzverCommand($from, $to, $format));
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 5
    public function sendCommand(CommandInterface $command)
219
    {
220 5
        return $this->connection->sendCommand($command);
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function sendArticle(CommandInterface $command)
227
    {
228
        return $this->connection->sendArticle($command);
229
    }
230
}
231