Passed
Push — master ( 5a4662...8f9c8a )
by Ross
02:40
created

GetZoneId::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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