Passed
Push — master ( 0c1970...c53e9c )
by Ross
02:31
created

ErrorFactory::createError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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
27
class ErrorFactory
28
{
29
    /**
30
     * @var Hydrator
31
     */
32
    private $hydrator;
33
34
    /**
35
     * ErrorFactory constructor.
36
     *
37
     * @param Hydrator $hydrator
38
     */
39 12
    public function __construct(Hydrator $hydrator)
40
    {
41 12
        $this->hydrator = $hydrator;
42 12
    }
43
44
    /**
45
     * @param array $errors
46
     *
47
     * @return Error[]
48
     */
49 10
    public function create(array $errors): array
50
    {
51 10
        $actualErrors = [];
52 10
        foreach ($errors as $error) {
53 10
            if(!\property_exists($error, 'code')) {
54 9
                continue;
55
            }
56 1
            $actualErrors[] = $this->createError($error);
57
        }
58
59 10
        return $actualErrors;
60
    }
61
62
    /**
63
     * @param \stdClass $error
64
     *
65
     * @return Error
66
     */
67 1
    private function createError(\stdClass $error): Error
68
    {
69 1
        $errorObject = new Error();
70 1
        foreach(['code', 'message'] as $property) {
71 1
            $this->hydrator->setProperty($errorObject, $error, $property);
72
        }
73
74 1
        return $errorObject;
75
    }
76
}
77