GetZoneId::getResult()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
eloc 4
nc 2
nop 0
crap 2
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\Model\Requests;
24
25
use RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException;
26
use RossMitchell\UpdateCloudFlare\Factories\Responses\ListZoneFactory;
27
use RossMitchell\UpdateCloudFlare\Interfaces\CurlInterface;
28
use RossMitchell\UpdateCloudFlare\Requests\ZoneInfo;
29
use RossMitchell\UpdateCloudFlare\Responses\ListZones;
30
use Symfony\Component\Console\Exception\LogicException;
31
32
/**
33
 * Class GetZoneId
34
 * @package RossMitchell\UpdateCloudFlare\Model\Requests
35
 */
36
class GetZoneId
37
{
38
    /**
39
     * @var CurlInterface
40
     */
41
    private $curl;
42
    /**
43
     * @var ZoneInfo
44
     */
45
    private $request;
46
    /**
47
     * @var array
48
     */
49
    private $zoneId = [];
50
    /**
51
     * @var ListZoneFactory
52
     */
53
    private $listZoneResults;
54
    /**
55
     * @var ListZones
56
     */
57
    private $result;
58
59
    /**
60
     * GetZoneId constructor.
61
     *
62
     * @param CurlInterface   $curl
63
     * @param ZoneInfo        $request
64
     * @param ListZoneFactory $listZoneResults
65
     */
66 3
    public function __construct(CurlInterface $curl, ZoneInfo $request, ListZoneFactory $listZoneResults)
67
    {
68 3
        $this->curl            = $curl;
69 3
        $this->request         = $request;
70 3
        $this->listZoneResults = $listZoneResults;
71 3
    }
72
73
    /**
74
     * @param int $index
75
     *
76
     * @return string
77
     * @throws LogicException
78
     * @throws CloudFlareException
79
     */
80 3
    public function getZoneId(int $index = 0): string
81
    {
82 3
        if (!isset($this->zoneId[$index])) {
83 3
            $this->zoneId[$index] = $this->getIdFromResult($index);
84
        }
85
86 1
        return $this->zoneId[$index];
87
    }
88
89
    /**
90
     * @param int $index
91
     *
92
     * @return string
93
     * @throws LogicException
94
     * @throws CloudFlareException
95
     */
96 3
    private function getIdFromResult(int $index): string
97
    {
98 3
        $result = $this->getResult();
99 2
        $zones  = $result->getResult();
100 2
        if (!isset($zones[$index])) {
101 1
            throw new LogicException("Zone $index is not defined");
102
        }
103 1
        $zone = $zones[$index];
104
105 1
        return $zone->getId();
106
    }
107
108
    /**
109
     * @return ListZones
110
     * @throws LogicException
111
     * @throws CloudFlareException
112
     */
113 3
    private function getResult(): ListZones
114
    {
115 3
        if ($this->result === null) {
116 3
            $rawResult    = \json_decode($this->curl->makeRequest($this->request));
117 3
            $this->result = $this->listZoneResults->create($rawResult);
118
        }
119
120 2
        return $this->result;
121
    }
122
}
123