Passed
Push — master ( 5adbb2...0c1970 )
by Ross
02:40
created

CloudFlareResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
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 Symfony\Component\Console\Exception\LogicException;
26
27
/**
28
 * Class CloudFlareResponse
29
 * @package RossMitchell\UpdateCloudFlare\Abstracts
30
 */
31
abstract class CloudFlareResponse
32
{
33
    /**
34
     * @var bool
35
     */
36
    private $success;
37
    /**
38
     * @var array
39
     */
40
    private $errors;
41
    /**
42
     * @var array
43
     */
44
    private $messages;
45
    private $resultInfo;
46
47
    /**
48
     * CloudFlareResponse constructor.
49
     *
50
     * @param \stdClass $result
51
     *
52
     * @throws LogicException
53
     */
54 11
    public function __construct(\stdClass $result)
55
    {
56 11
        $this->success    = (bool) $this->getNode($result, 'success');
57 10
        $this->resultInfo = $this->getNode($result, 'result_info', false);
58 10
        $this->setMessages($this->getNode($result, 'messages'));
59 9
        $this->setErrors($this->getNode($result, 'errors'));
60 8
        $this->setResult($this->getNode($result, 'result'));
61 7
    }
62
63
    /**
64
     * @param $result
65
     */
66
    abstract public function setResult($result);
67
68
    /**
69
     * @return mixed
70
     */
71
    abstract public function getResult();
72
73
    /**
74
     * @return bool
75
     */
76 1
    public function isSuccess(): bool
77
    {
78 1
        return $this->success;
79
    }
80
81
    /**
82
     * @return array
83
     */
84 1
    public function getErrors(): array
85
    {
86 1
        return $this->errors;
87
    }
88
89
    /**
90
     * @return array
91
     */
92 1
    public function getMessages(): array
93
    {
94 1
        return $this->messages;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100 2
    public function getResultInfo()
101
    {
102 2
        return $this->resultInfo;
103
    }
104
105
    /**
106
     *
107
     *
108
     * @param array $errors
109
     */
110 8
    private function setErrors(array $errors)
111
    {
112 8
        $this->errors = $this->stripEmptyObjectsFromArray($errors);
113 8
    }
114
115
    /**
116
     * @param array $messages
117
     */
118 9
    private function setMessages(array $messages)
119
    {
120 9
        $this->messages = $this->stripEmptyObjectsFromArray($messages);
121 9
    }
122
123
    /**
124
     * The example JSON returns an array of empty classes. I don't want that to pollute the arrays, so we'll remove
125
     * them here.
126
     *
127
     * @param array $array
128
     *
129
     * @return array
130
     */
131 9
    private function stripEmptyObjectsFromArray(array $array): array
132
    {
133 9
        $cleanedArray = [];
134 9
        foreach ($array as $item) {
135 9
            if (\json_encode($item) === '{}') {
136 9
                continue;
137
            }
138
            $cleanedArray[] = $item;
139
        }
140
141 9
        return $cleanedArray;
142
    }
143
144
    /**
145
     * @param \stdClass $result
146
     * @param string    $node
147
     * @param bool      $required
148
     *
149
     * @return mixed
150
     * @throws \Symfony\Component\Console\Exception\LogicException
151
     */
152 11
    private function getNode(\stdClass $result, string $node, $required = true)
153
    {
154 11
        if (!\property_exists($result, $node)) {
155 5
            if ($required === true) {
156 4
                throw new LogicException("$node does not exist in the result");
157
            }
158
159 1
            return null;
160
        }
161
162 10
        return $result->$node;
163
    }
164
}
165