|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Beñat Espiña <[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 BenatEspina\StackExchangeApiClient\Application\Service\Answer; |
|
13
|
|
|
|
|
14
|
|
|
use BenatEspina\StackExchangeApiClient\Application\DataTransformer\ResponseAnswerDataTransformer; |
|
15
|
|
|
use BenatEspina\StackExchangeApiClient\Domain\Model\Authentication; |
|
16
|
|
|
use BenatEspina\StackExchangeApiClient\Domain\Model\Http; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Answers of ids command handler. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Beñat Espiña <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
class AnswersOfIdsHandler |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The data transformer. |
|
27
|
|
|
* |
|
28
|
|
|
* @var ResponseAnswerDataTransformer |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dataTransformer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The HTTP domain class. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Http |
|
36
|
|
|
*/ |
|
37
|
|
|
private $http; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The authentication, it is not mandatory. |
|
41
|
|
|
* |
|
42
|
|
|
* @var Authentication|null |
|
43
|
|
|
*/ |
|
44
|
|
|
private $authentication; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Http $http The HTTP domain class |
|
50
|
|
|
* @param ResponseAnswerDataTransformer $dataTransformer The answer data transformer |
|
51
|
|
|
* @param Authentication|null $anAuthentication The authentication, it is not mandatory |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
Http $http, |
|
55
|
|
|
ResponseAnswerDataTransformer $dataTransformer, |
|
56
|
|
|
Authentication $anAuthentication = null |
|
57
|
|
|
) { |
|
58
|
|
|
$this->http = $http; |
|
59
|
|
|
$this->dataTransformer = $dataTransformer; |
|
60
|
|
|
$this->authentication = $anAuthentication; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get answers identified by a set of ids. |
|
65
|
|
|
* |
|
66
|
|
|
* More info: http://api.stackexchange.com/docs/answers-by-ids |
|
67
|
|
|
* |
|
68
|
|
|
* @param AnswersOfIdsCommand $command The command |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function handle(AnswersOfIdsCommand $command) |
|
73
|
|
|
{ |
|
74
|
|
|
$parameters = $command->params(); |
|
75
|
|
|
if ($this->authentication instanceof Authentication) { |
|
76
|
|
|
$parameters = array_merge($parameters, $this->authentication->toArray()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$response = $this->http->get($command->url(), $parameters); |
|
80
|
|
|
$this->dataTransformer->write($response); |
|
81
|
|
|
|
|
82
|
|
|
return $this->dataTransformer->read(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.