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() |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 4 | public function authenticate($username, $password = null) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function connectAndAuthenticate($username = null, $password = null) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 5 | public function authInfo($type, $value) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function body($article) |
||
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) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 1 | public function help() |
|
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)); |
||
|
|||
181 | } |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 1 | public function quit() |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 1 | public function xfeature($feature) |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | 1 | public function xover($from, $to, array $format) |
|
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 1 | public function xzver($from, $to, array $format) |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 5 | public function sendCommand(CommandInterface $command) |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function sendArticle(CommandInterface $command) |
||
230 | } |
||
231 |
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.