ErrorFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php declare(strict_types = 1);
2
/**
3
 *
4
 * Copyright (C) 2018  Ross Mitchell
5
 *
6
 * This file is part of RossMitchell/UpdateCloudFlare.
7
 *
8
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
9
 * it and/or modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace RossMitchell\UpdateCloudFlare\Factories\Responses;
23
24
use RossMitchell\UpdateCloudFlare\Helpers\Hydrator;
25
use RossMitchell\UpdateCloudFlare\Responses\Error;
26
use Symfony\Component\Console\Exception\LogicException;
27
28
/**
29
 * Class ErrorFactory
30
 * @package RossMitchell\UpdateCloudFlare\Factories\Responses
31
 */
32
class ErrorFactory
33
{
34
    /**
35
     * @var Hydrator
36
     */
37
    private $hydrator;
38
39
    /**
40
     * ErrorFactory constructor.
41
     *
42
     * @param Hydrator $hydrator
43
     */
44 54
    public function __construct(Hydrator $hydrator)
45
    {
46 54
        $this->hydrator = $hydrator;
47 54
    }
48
49
    /**
50
     * @param array $errors
51
     *
52
     * @return Error[]
53
     * @throws LogicException
54
     */
55 46
    public function create(array $errors): array
56
    {
57 46
        $actualErrors = [];
58 46
        foreach ($errors as $error) {
59 46
            if (!\property_exists($error, 'code')) {
60 40
                continue;
61
            }
62 7
            $actualErrors[] = $this->createError($error);
63
        }
64
65 46
        return $actualErrors;
66
    }
67
68
    /**
69
     * @param \stdClass $error
70
     *
71
     * @return Error
72
     * @throws LogicException
73
     */
74 7
    private function createError(\stdClass $error): Error
75
    {
76 7
        $errorObject = new Error();
77 7
        foreach (['code', 'message'] as $property) {
78 7
            $this->hydrator->setProperty($errorObject, $error, $property);
79
        }
80
81 7
        return $errorObject;
82
    }
83
}
84