Passed
Push — master ( ac5429...699403 )
by Ross
02:06
created

CloudFlareResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 97
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNode() 0 11 3
A getErrors() 0 3 1
A isSuccess() 0 3 1
A getMessages() 0 3 1
A __construct() 0 11 2
A getResultInfo() 0 3 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 string $rawResult
51
     *
52
     * @throws LogicException
53
     */
54
    public function __construct(string $rawResult)
55
    {
56
        $result = \json_decode($rawResult);
57
        if ($result === false) {
58
            throw new LogicException('Could not decode the result');
59
        }
60
        $this->success    = (bool)$this->getNode($result, 'success');
61
        $this->errors     = $this->getNode($result, 'errors');
62
        $this->messages   = $this->getNode($result, 'messages');
63
        $this->resultInfo = $this->getNode($result, 'result_info', false);
64
        $this->setResults($this->getNode($result, 'result'));
0 ignored issues
show
Bug introduced by
The method setResults() does not exist on RossMitchell\UpdateCloud...acts\CloudFlareResponse. Did you maybe mean setResult()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $this->/** @scrutinizer ignore-call */ 
65
               setResults($this->getNode($result, 'result'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
    }
66
67
    /**
68
     * @param $result
69
     */
70
    abstract public function setResult($result);
71
72
    /**
73
     * @return mixed
74
     */
75
    abstract public function getResult();
76
77
    /**
78
     * @return bool
79
     */
80
    public function isSuccess(): bool
81
    {
82
        return $this->success;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getErrors(): array
89
    {
90
        return $this->errors;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getMessages(): array
97
    {
98
        return $this->messages;
99
    }
100
101
    /**
102
     * @return null
103
     */
104
    public function getResultInfo()
105
    {
106
        return $this->resultInfo;
107
    }
108
109
110
    /**
111
     * @param \stdClass $result
112
     * @param string    $node
113
     * @param bool      $required
114
     *
115
     * @return mixed
116
     */
117
    private function getNode(\stdClass $result, string $node, $required = true)
118
    {
119
        if (!\property_exists($result, $node)) {
120
            if ($required === true) {
121
                throw new LogicException("$node does not exist in the result");
122
            }
123
124
            return null;
125
        }
126
127
        return $result->$node;
128
    }
129
}
130