1 | <?php |
||
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) |
|
38 | |||
39 | /** |
||
40 | * Get the connection instance. |
||
41 | * |
||
42 | * @return ConnectionInterface |
||
43 | */ |
||
44 | 1 | public function getConnection() |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 1 | public function connect() |
|
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) |
|
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) |
|
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 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function help() |
|
139 | { |
||
140 | 1 | return $this->sendCommand(new Command\HelpCommand()); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 1 | public function overviewFormat() |
|
147 | { |
||
148 | 1 | return $this->sendCommand(new Command\OverviewFormatCommand()); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function post($groups, $subject, $body, $from, $headers = null) |
||
155 | { |
||
156 | $command = $this->sendCommand(new Command\PostCommand()); |
||
157 | $response = $command->getResponse(); |
||
158 | |||
159 | if ($response->getStatusCode() === Response::SEND_ARTICLE) { |
||
160 | $command = $this->postArticle($groups, $subject, $body, $from, $headers); |
||
161 | $response = $command->getResponse(); |
||
162 | } |
||
163 | |||
164 | if ($response->getStatusCode() !== Response::ARTICLE_RECEIVED) { |
||
165 | throw new RuntimeException(sprintf('Posting failed: %s', (string) $response)); |
||
166 | } |
||
167 | |||
168 | return $response; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function postArticle($groups, $subject, $body, $from, $headers = null) |
||
175 | { |
||
176 | return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers)); |
||
|
|||
177 | } |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 1 | public function quit() |
|
183 | { |
||
184 | 1 | return $this->sendCommand(new Command\QuitCommand()); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 1 | public function xfeature($feature) |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 1 | public function xover($from, $to, array $format) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | 1 | public function xzver($from, $to, array $format) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 5 | public function sendCommand(CommandInterface $command) |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function sendArticle(CommandInterface $command) |
||
226 | } |
||
227 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.