CloudFlareResponse::getNode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 3
crap 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Abstracts;
24
25
use RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException;
26
use RossMitchell\UpdateCloudFlare\Factories\Responses\ErrorFactory;
27
use RossMitchell\UpdateCloudFlare\Responses\Error;
28
use Symfony\Component\Console\Exception\LogicException;
29
30
/**
31
 * Class CloudFlareResponse
32
 * @package RossMitchell\UpdateCloudFlare\Abstracts
33
 */
34
abstract class CloudFlareResponse
35
{
36
    /**
37
     * @var bool
38
     */
39
    private $success;
40
    /**
41
     * @var array
42
     */
43
    private $errors;
44
    /**
45
     * @var array
46
     */
47
    private $messages;
48
    private $resultInfo;
49
    /**
50
     * @var ErrorFactory
51
     */
52
    private $errorFactory;
53
54
    /**
55
     * CloudFlareResponse constructor.
56
     *
57
     * @param \stdClass    $result
58
     * @param ErrorFactory $errorFactory
59
     *
60
     * @throws LogicException
61
     * @throws CloudFlareException
62
     */
63 49
    public function __construct(\stdClass $result, ErrorFactory $errorFactory)
64
    {
65 49
        $this->errorFactory = $errorFactory;
66 49
        $this->success      = (bool) $this->getNode($result, 'success');
67 46
        $this->setErrors($this->getNode($result, 'errors'));
68 43
        if ($this->isSuccess() !== true) {
69 7
            $exception = new CloudFlareException();
70 7
            $exception->setDetails($this);
71
72 7
            throw $exception;
73
        }
74 36
        $this->resultInfo = $this->getNode($result, 'result_info', false);
75 36
        $this->setMessages($this->getNode($result, 'messages'));
76 33
        $this->setResult($this->getNode($result, 'result'));
77 30
    }
78
79
    /**
80
     * @param mixed $result
81
     */
82
    abstract public function setResult($result): void;
83
84
    /**
85
     * @return mixed
86
     */
87
    abstract public function getResult();
88
89
    /**
90
     * @return bool
91
     */
92 43
    public function isSuccess(): bool
93
    {
94 43
        return $this->success;
95
    }
96
97
    /**
98
     * @return Error[]
99
     */
100 10
    public function getErrors(): array
101
    {
102 10
        return $this->errors;
103
    }
104
105
    /**
106
     * @return array
107
     */
108 4
    public function getMessages(): array
109
    {
110 4
        return $this->messages;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116 6
    public function getResultInfo()
117
    {
118 6
        return $this->resultInfo;
119
    }
120
121
    /**
122
     *
123
     *
124
     * @param array $errors
125
     *
126
     * @throws LogicException
127
     */
128 43
    private function setErrors(array $errors): void
129
    {
130 43
        $this->errors = $this->errorFactory->create($errors);
131 43
    }
132
133
    /**
134
     * @param array $messages
135
     */
136 33
    private function setMessages(array $messages): void
137
    {
138 33
        $this->messages = $this->stripEmptyObjectsFromArray($messages);
139 33
    }
140
141
    /**
142
     * The example JSON returns an array of empty classes. I don't want that to pollute the arrays, so we'll remove
143
     * them here.
144
     *
145
     * @param array $array
146
     *
147
     * @return array
148
     */
149 33
    private function stripEmptyObjectsFromArray(array $array): array
150
    {
151 33
        $cleanedArray = [];
152 33
        foreach ($array as $item) {
153 33
            if (\json_encode($item) === '{}') {
154 33
                continue;
155
            }
156 1
            $cleanedArray[] = $item;
157
        }
158
159 33
        return $cleanedArray;
160
    }
161
162
    /**
163
     * @param \stdClass $result
164
     * @param string    $node
165
     * @param bool      $required
166
     *
167
     * @return mixed
168
     * @throws LogicException
169
     */
170 49
    private function getNode(\stdClass $result, string $node, $required = true)
171
    {
172 49
        if (!\property_exists($result, $node)) {
173 15
            if ($required === true) {
174 12
                throw new LogicException("$node does not exist in the result");
175
            }
176
177 3
            return null;
178
        }
179
180 46
        return $result->$node;
181
    }
182
}
183