Completed
Push — v3 ( d12fea )
by Beñat
05:39
created

AcceptAnswerHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace BenatEspina\StackExchangeApiClient\Application\Service\Answer;
4
5
use BenatEspina\StackExchangeApiClient\Application\DataTransformer\ResponseAnswerDataTransformer;
6
use BenatEspina\StackExchangeApiClient\Domain\Model\Authentication;
7
use BenatEspina\StackExchangeApiClient\Domain\Model\Http;
8
9
/**
10
 * Accept answer command handler.
11
 *
12
 * @author Beñat Espiña <[email protected]>
13
 */
14
class AcceptAnswerHandler
15
{
16
    /**
17
     * The authentication.
18
     *
19
     * @var Authentication
20
     */
21
    private $authentication;
22
23
    /**
24
     * The data transformer.
25
     *
26
     * @var ResponseAnswerDataTransformer
27
     */
28
    private $dataTransformer;
29
30
    /**
31
     * The HTTP domain class.
32
     *
33
     * @var Http
34
     */
35
    private $http;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param Http                          $http            The HTTP domain class
41
     * @param ResponseAnswerDataTransformer $dataTransformer The answer data transformer
42
     * @param Authentication                $authentication  The authentication
43
     */
44
    public function __construct(
45
        Http $http,
46
        ResponseAnswerDataTransformer $dataTransformer,
47
        Authentication $authentication
48
    ) {
49
        $this->authentication = $authentication;
50
        $this->dataTransformer = $dataTransformer;
51
        $this->http = $http;
52
    }
53
54
    /**
55
     * Casts an accept vote on the given answer.
56
     *
57
     * More info: https://api.stackexchange.com/docs/accept-answer
58
     *
59
     * @param AcceptAnswerCommand $command The command
60
     *
61
     * @return mixed
62
     */
63
    public function handle(AcceptAnswerCommand $command)
64
    {
65
        $response = $this->http->put(
66
            $command->url(),
67
            array_merge($command->params(), $this->authentication->toArray())
68
        );
69
        $this->dataTransformer->write($response);
70
71
        return $this->dataTransformer->read();
72
    }
73
}
74