Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class Transmission |
||
15 | { |
||
16 | /** |
||
17 | * @var Client |
||
18 | */ |
||
19 | protected $client; |
||
20 | |||
21 | /** |
||
22 | * @var ResponseValidator |
||
23 | */ |
||
24 | protected $validator; |
||
25 | |||
26 | /** |
||
27 | * @var PropertyMapper |
||
28 | */ |
||
29 | protected $mapper; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param string $host |
||
35 | * @param integer $port |
||
36 | * @param string $path |
||
37 | */ |
||
38 | public function __construct($host = null, $port = null, $path = null) |
||
39 | { |
||
40 | $this->setClient(new Client($host, $port, $path)); |
||
41 | $this->setMapper(new PropertyMapper()); |
||
42 | $this->setValidator(new ResponseValidator()); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get all the torrents in the download queue |
||
47 | * |
||
48 | * @return Torrent[] |
||
49 | */ |
||
50 | public function all($customFields = null) |
||
51 | { |
||
52 | $fields = isset($customFields) ? $customFields : Torrent::getMapping(); |
||
53 | $client = $this->getClient(); |
||
54 | $mapper = $this->getMapper(); |
||
55 | $response = $this->getClient()->call( |
||
56 | 'torrent-get', |
||
57 | array('fields' => array_keys($fields) |
||
58 | ); |
||
|
|||
59 | |||
60 | $torrents = array_map(function ($data) use ($mapper, $client) { |
||
61 | return $mapper->map( |
||
62 | new Torrent($client), |
||
63 | $data |
||
64 | ); |
||
65 | }, $this->getValidator()->validate('torrent-get', $response)); |
||
66 | |||
67 | return $torrents; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get a specific torrent from the download queue |
||
72 | * |
||
73 | * @param integer $id |
||
74 | * @return Torrent |
||
75 | * @throws \RuntimeException |
||
76 | */ |
||
77 | public function get($id) |
||
78 | { |
||
79 | $client = $this->getClient(); |
||
80 | $mapper = $this->getMapper(); |
||
81 | $response = $this->getClient()->call('torrent-get', array( |
||
82 | 'fields' => array_keys(Torrent::getMapping()), |
||
83 | 'ids' => array($id) |
||
84 | )); |
||
85 | |||
86 | $torrent = array_reduce( |
||
87 | $this->getValidator()->validate('torrent-get', $response), |
||
88 | function ($torrent, $data) use ($mapper, $client) { |
||
89 | return $torrent ? $torrent : $mapper->map(new Torrent($client), $data); |
||
90 | }); |
||
91 | |||
92 | if (!$torrent instanceof Torrent) { |
||
93 | throw new \RuntimeException(sprintf("Torrent with ID %s not found", $id)); |
||
94 | } |
||
95 | |||
96 | return $torrent; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get the Transmission session |
||
101 | * |
||
102 | * @return Session |
||
103 | */ |
||
104 | public function getSession() |
||
105 | { |
||
106 | $response = $this->getClient()->call( |
||
107 | 'session-get', |
||
108 | array() |
||
109 | ); |
||
110 | |||
111 | return $this->getMapper()->map( |
||
112 | new Session($this->getClient()), |
||
113 | $this->getValidator()->validate('session-get', $response) |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return SessionStats |
||
119 | */ |
||
120 | public function getSessionStats() |
||
121 | { |
||
122 | $response = $this->getClient()->call( |
||
123 | 'session-stats', |
||
124 | array() |
||
125 | ); |
||
126 | |||
127 | return $this->getMapper()->map( |
||
128 | new SessionStats(), |
||
129 | $this->getValidator()->validate('session-stats', $response) |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get Free space |
||
135 | * @param string $path |
||
136 | * @return FreeSpace |
||
137 | */ |
||
138 | public function getFreeSpace($path=null) |
||
139 | { |
||
140 | if (!$path) { |
||
141 | $path = $this->getSession()->getDownloadDir(); |
||
142 | } |
||
143 | $response = $this->getClient()->call( |
||
144 | 'free-space', |
||
145 | array('path'=>$path) |
||
146 | ); |
||
147 | |||
148 | return $this->getMapper()->map( |
||
149 | new FreeSpace(), |
||
150 | $this->getValidator()->validate('free-space', $response) |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Add a torrent to the download queue |
||
156 | * |
||
157 | * @param string $torrent |
||
158 | * @param boolean $metainfo |
||
159 | * @param string $savepath |
||
160 | * @return Torrent |
||
161 | */ |
||
162 | public function add($torrent, $metainfo = false, $savepath = null) |
||
163 | { |
||
164 | $parameters = array($metainfo ? 'metainfo' : 'filename' => $torrent); |
||
165 | |||
166 | if ($savepath !== null) { |
||
167 | $parameters['download-dir'] = (string) $savepath; |
||
168 | } |
||
169 | |||
170 | $response = $this->getClient()->call( |
||
171 | 'torrent-add', |
||
172 | $parameters |
||
173 | ); |
||
174 | |||
175 | return $this->getMapper()->map( |
||
176 | new Torrent($this->getClient()), |
||
177 | $this->getValidator()->validate('torrent-add', $response) |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Start the download of a torrent |
||
183 | * |
||
184 | * @param Torrent $torrent |
||
185 | * @param bool $now |
||
186 | */ |
||
187 | public function start(Torrent $torrent, $now = false) |
||
188 | { |
||
189 | $this->getClient()->call( |
||
190 | $now ? 'torrent-start-now' : 'torrent-start', |
||
191 | array('ids' => array($torrent->getId())) |
||
192 | ); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Stop the download of a torrent |
||
197 | * |
||
198 | * @param Torrent $torrent |
||
199 | */ |
||
200 | public function stop(Torrent $torrent) |
||
201 | { |
||
202 | $this->getClient()->call( |
||
203 | 'torrent-stop', |
||
204 | array('ids' => array($torrent->getId())) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Verify the download of a torrent |
||
210 | * |
||
211 | * @param Torrent $torrent |
||
212 | */ |
||
213 | public function verify(Torrent $torrent) |
||
214 | { |
||
215 | $this->getClient()->call( |
||
216 | 'torrent-verify', |
||
217 | array('ids' => array($torrent->getId())) |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Request a reannounce of a torrent |
||
223 | * |
||
224 | * @param Torrent $torrent |
||
225 | */ |
||
226 | public function reannounce(Torrent $torrent) |
||
227 | { |
||
228 | $this->getClient()->call( |
||
229 | 'torrent-reannounce', |
||
230 | array('ids' => array($torrent->getId())) |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Remove a torrent from the download queue |
||
236 | * |
||
237 | * @param Torrent $torrent |
||
238 | */ |
||
239 | public function remove(Torrent $torrent, $localData = false) |
||
240 | { |
||
241 | $arguments = array('ids' => array($torrent->getId())); |
||
242 | |||
243 | if ($localData) { |
||
244 | $arguments['delete-local-data'] = true; |
||
245 | } |
||
246 | |||
247 | $this->getClient()->call('torrent-remove', $arguments); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Set the client used to connect to Transmission |
||
252 | * |
||
253 | * @param Client $client |
||
254 | */ |
||
255 | public function setClient(Client $client) |
||
256 | { |
||
257 | $this->client = $client; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Get the client used to connect to Transmission |
||
262 | * |
||
263 | * @return Client |
||
264 | */ |
||
265 | public function getClient() |
||
266 | { |
||
267 | return $this->client; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Set the hostname of the Transmission server |
||
272 | * |
||
273 | * @param string $host |
||
274 | */ |
||
275 | public function setHost($host) |
||
276 | { |
||
277 | $this->getClient()->setHost($host); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get the hostname of the Transmission server |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public function getHost() |
||
286 | { |
||
287 | return $this->getClient()->getHost(); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Set the port the Transmission server is listening on |
||
292 | * |
||
293 | * @param integer $port |
||
294 | */ |
||
295 | public function setPort($port) |
||
296 | { |
||
297 | $this->getClient()->setPort($port); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get the port the Transmission server is listening on |
||
302 | * |
||
303 | * @return integer |
||
304 | */ |
||
305 | public function getPort() |
||
306 | { |
||
307 | return $this->getClient()->getPort(); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set the mapper used to map responses from Transmission to models |
||
312 | * |
||
313 | * @param PropertyMapper $mapper |
||
314 | */ |
||
315 | public function setMapper(PropertyMapper $mapper) |
||
316 | { |
||
317 | $this->mapper = $mapper; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Get the mapper used to map responses from Transmission to models |
||
322 | * |
||
323 | * @return PropertyMapper |
||
324 | */ |
||
325 | public function getMapper() |
||
326 | { |
||
327 | return $this->mapper; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Set the validator used to validate Transmission responses |
||
332 | * |
||
333 | * @param ResponseValidator $validator |
||
334 | */ |
||
335 | public function setValidator(ResponseValidator $validator) |
||
336 | { |
||
337 | $this->validator = $validator; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Get the validator used to validate Transmission responses |
||
342 | * |
||
343 | * @return ResponseValidator |
||
344 | */ |
||
345 | public function getValidator() |
||
346 | { |
||
347 | return $this->validator; |
||
348 | } |
||
349 | } |
||
350 |