Hydrator::setProperty()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 4
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\Helpers;
24
25
use Symfony\Component\Console\Exception\LogicException;
26
27
/**
28
 * Class Hydrator
29
 * @package RossMitchell\UpdateCloudFlare\Helpers
30
 */
31
class Hydrator
32
{
33
    /**
34
     * @param object    $class
35
     * @param \stdClass $data
36
     * @param string    $node
37
     * @param bool      $required
38
     *
39
     * @throws LogicException
40
     */
41 124
    public function setProperty($class, \stdClass $data, string $node, $required = true): void
42
    {
43 124
        if (!\property_exists($data, $node)) {
44 39
            if ($required === true) {
45 38
                throw new LogicException("$node does not exist in the result");
46
            }
47 1
            $data->$node = null;
48
        }
49
50 119
        $settor = 'set' . str_replace('_', '', ucwords($node, '_'));
51
52 119
        if (!\method_exists($class, $settor)) {
53 1
            throw new LogicException("method $settor does not exist in class " . \get_class($class));
54
        }
55
56 118
        $class->$settor($data->$node);
57 118
    }
58
}
59