AddOrUpdateTranslationsRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php // Copyright ⓒ 2018 Magneds IP B.V. - All Rights Reserved
2
namespace Magneds\Lokalise\TranslateString\Request;
3
4
use Magneds\Lokalise\Project\Entity\ProjectID;
5
use Magneds\Lokalise\RequestInterface;
6
use Magneds\Lokalise\ResponseInfo;
7
use Magneds\Lokalise\TranslateString\Entity\Result;
8
use Magneds\Lokalise\TranslateString\Entity\TranslateStringPartial;
9
use Psr\Http\Message\ResponseInterface;
10
11
class AddOrUpdateTranslationsRequest implements RequestInterface
12
{
13
    /**
14
     * @var ProjectID
15
     */
16
    protected $projectID;
17
18
    /**
19
     * @var TranslateStringPartial[]
20
     */
21
    protected $data = [];
22
23
    /**
24
     * AddOrUpdateTranslationsRequest constructor.
25
     * @param ProjectID $projectID
26
     */
27
    public function __construct(ProjectID $projectID)
28
    {
29
        $this->projectID = $projectID;
30
    }
31
32
    public function addData(TranslateStringPartial $partial)
33
    {
34
        $this->data[] = $partial;
35
        return $this;
36
    }
37
38
    /**
39
     * Return the method to make this request in.
40
     *
41
     * @return string
42
     */
43
    public function getMethod()
44
    {
45
        return 'POST';
46
    }
47
48
    /**
49
     * This URI is appended to the route URL given to the Client object.
50
     *
51
     * @return string
52
     */
53
    public function getURI()
54
    {
55
        return 'string/set';
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getQueryArguments()
62
    {
63
        return [];
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getBody()
70
    {
71
        $data = [];
72
        foreach ($this->data as $partial) {
73
            $data[] = $partial->toArray();
74
        }
75
76
        $d = [
77
            'id'   => $this->projectID->getID(),
78
            'data' => json_encode($data)
79
        ];
80
81
        return $d;
82
    }
83
84
    /**
85
     * @param ResponseInterface $response
86
     * @return ResponseInfo
87
     */
88
    public function handleResponse(ResponseInterface $response): ResponseInfo
89
    {
90
        $responseData = json_decode($response->getBody()->getContents(), true);
91
        $responseInfo = ResponseInfo::buildFromArray($responseData['response']);
92
93
        if($responseInfo->getCode() !== 200) {
94
            return $responseInfo;
95
        }
96
97
        $responseInfo->setActionData(Result::buildFromArray($responseData['result']));
98
        return $responseInfo;
99
    }
100
}
101