Completed
Push — v3 ( d12fea...862d0b )
by Beñat
03:42
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
/*
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
 * Accept answer command handler.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class AcceptAnswerHandler
24
{
25
    /**
26
     * The authentication.
27
     *
28
     * @var Authentication
29
     */
30
    private $authentication;
31
32
    /**
33
     * The data transformer.
34
     *
35
     * @var ResponseAnswerDataTransformer
36
     */
37
    private $dataTransformer;
38
39
    /**
40
     * The HTTP domain class.
41
     *
42
     * @var Http
43
     */
44
    private $http;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param Http                          $http            The HTTP domain class
50
     * @param ResponseAnswerDataTransformer $dataTransformer The answer data transformer
51
     * @param Authentication                $authentication  The authentication
52
     */
53
    public function __construct(
54
        Http $http,
55
        ResponseAnswerDataTransformer $dataTransformer,
56
        Authentication $authentication
57
    ) {
58
        $this->authentication = $authentication;
59
        $this->dataTransformer = $dataTransformer;
60
        $this->http = $http;
61
    }
62
63
    /**
64
     * Casts an accept vote on the given answer.
65
     *
66
     * More info: https://api.stackexchange.com/docs/accept-answer
67
     *
68
     * @param AcceptAnswerCommand $command The command
69
     *
70
     * @return mixed
71
     */
72
    public function handle(AcceptAnswerCommand $command)
73
    {
74
        $response = $this->http->put(
75
            $command->url(),
76
            array_merge($command->params(), $this->authentication->toArray())
77
        );
78
        $this->dataTransformer->write($response);
79
80
        return $this->dataTransformer->read();
81
    }
82
}
83