Completed
Pull Request — master (#20)
by
unknown
01:46
created

Client::disconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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