ListZoneResultsFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
ccs 11
cts 11
cp 1
cc 4
eloc 10
nc 6
nop 1
crap 4
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\Factories\Responses\Results;
24
25
use RossMitchell\UpdateCloudFlare\Helpers\Hydrator;
26
use RossMitchell\UpdateCloudFlare\Responses\Results\ListZonesResult;
27
use Symfony\Component\Console\Exception\LogicException;
28
29
/**
30
 * Class ListZoneResultsFactory
31
 * @package RossMitchell\UpdateCloudFlare\Factories\Responses\Results
32
 */
33
class ListZoneResultsFactory
34
{
35
    public const COMPLEX_PROPERTIES = ['owner', 'plan', 'plan_pending'];
36
    public const SIMPLE_PROPERTIES  = [
37
        'id',
38
        'name',
39
        'development_mode',
40
        'original_name_servers',
41
        'original_registrar',
42
        'original_dnshost',
43
        'created_on',
44
        'modified_on',
45
        'permissions',
46
        'status',
47
        'paused',
48
        'type',
49
        'name_servers',
50
    ];
51
52
    /**
53
     * @var Hydrator
54
     */
55
    private $hydrator;
56
    /**
57
     * @var OwnerFactory
58
     */
59
    private $ownerFactory;
60
    /**
61
     * @var PlanFactory
62
     */
63
    private $planFactory;
64
65
    /**
66
     * ListZoneResultsFactory constructor.
67
     *
68
     * @param Hydrator     $hydrator
69
     * @param OwnerFactory $ownerFactory
70
     * @param PlanFactory  $planFactory
71
     */
72 49
    public function __construct(Hydrator $hydrator, OwnerFactory $ownerFactory, PlanFactory $planFactory)
73
    {
74 49
        $this->hydrator     = $hydrator;
75 49
        $this->ownerFactory = $ownerFactory;
76 49
        $this->planFactory  = $planFactory;
77 49
    }
78
79
    /**
80
     * @param \stdClass $data
81
     *
82
     * @return ListZonesResult
83
     * @throws LogicException
84
     */
85 42
    public function create(\stdClass $data): ListZonesResult
86
    {
87 42
        $listZone = new ListZonesResult();
88 42
        foreach (self::SIMPLE_PROPERTIES as $property) {
89 42
            $this->hydrator->setProperty($listZone, $data, $property);
90
        }
91
92 29
        foreach (self::COMPLEX_PROPERTIES as $property) {
93 29
            if (!\property_exists($data, $property)) {
94 3
                throw new LogicException("No $property node in the response");
95
            }
96
        }
97
98 26
        $listZone->setOwner($this->ownerFactory->create($data->owner));
99 26
        $listZone->setPlan($this->planFactory->create($data->plan));
100 26
        $listZone->setPlanPending($this->planFactory->create($data->plan_pending));
101
102 26
        return $listZone;
103
    }
104
}
105