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

GetZoneId::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\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
    public function __construct(CurlInterface $curl, ZoneInfo $request, ListZoneFactory $listZoneResults)
66
    {
67
        $this->curl            = $curl;
68
        $this->request         = $request;
69
        $this->listZoneResults = $listZoneResults;
70
    }
71
72
    /**
73
     * @param int $index
74
     *
75
     * @return string
76
     * @throws LogicException
77
     */
78
    public function getZoneId(int $index = 0): string
79
    {
80
        if (!isset($this->zoneId[$index])) {
81
            $this->zoneId[$index] = $this->getIdFromResult($index);
82
        }
83
84
        return $this->zoneId[$index];
85
    }
86
87
    /**
88
     * @param int $index
89
     *
90
     * @return string
91
     * @throws LogicException
92
     */
93
    private function getIdFromResult(int $index = 0): string
94
    {
95
        $result = $this->getResult();
96
        $zones  = $result->getResult();
97
        if (empty($zones)) {
98
            throw new LogicException('No Zones Defined');
99
        }
100
        if (!isset($zones[$index])) {
101
            throw new LogicException("Zone $index is not defined");
102
        }
103
        $zone = $zones[$index];
104
105
        return $zone->getId();
106
    }
107
108
    /**
109
     * @return ListZones
110
     */
111
    private function getResult(): ListZones
112
    {
113
        if ($this->result === null) {
114
            $rawResult    = $this->curl->makeRequest($this->request);
115
            $this->result = $this->listZoneResults->create($rawResult);
116
        }
117
118
        return $this->result;
119
    }
120
}
121