Completed
Push — master ( 6986c5...be73e7 )
by Vitalijs
02:50
created

DTOBaseBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 62
ccs 29
cts 29
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 14 4
B buildFromData() 0 12 6
A buildFromJson() 0 12 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: vlitvinovs
5
 * Date: 2/8/17
6
 * Time: 4:28 PM
7
 */
8
9
namespace CodinPro\DataTransferObject;
10
11
class DTOBaseBuilder
12
{
13 40
    public function __construct(DTOBase $dtoBase)
14
    {
15 40
        $this->dto = $dtoBase;
0 ignored issues
show
Bug introduced by
The property dto does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16 40
    }
17
18
    /**
19
     * Build DTO from given type of data
20
     * @param $data
21
     * @throws \InvalidArgumentException
22
     */
23 40
    public function build($data)
24
    {
25 40
        switch (gettype($data)) {
26 40
            case 'array':
27 10
            case 'object':
28 33
                $this->buildFromData($data);
29 33
                break;
30 8
            case 'string':
31 6
                $this->buildFromJson($data);
32 4
                break;
33
            default:
34 2
                throw new \InvalidArgumentException('DTO can be built from array|object|json, "'.gettype($data).'" given.');
35
        }
36 36
    }
37
38
    /**
39
     * Build DTO from provided data
40
     * @param object|array $data
41
     */
42 36
    private function buildFromData($data)
43
    {
44 36
        foreach ($this->dto->getDefault() as $key => $value) {
45 34
            if (is_object($data) && isset($data->{$key})) {
46 6
                $this->dto[$key] = $data->{$key};
47 34
            } else if (is_array($data) && isset($data[$key])) {
48 13
                $this->dto[$key] = $data[$key];
49
            } else {
50 34
                $this->dto[$key] = $value;
51
            }
52
        }
53 36
    }
54
55
    /**
56
     * Try to build from provided string as JSON
57
     * @param string $data
58
     * @throws \InvalidArgumentException
59
     */
60 6
    private function buildFromJson($data)
61
    {
62 6
        $triedToDecodeData = json_decode($data);
63
64 6
        if ($triedToDecodeData !== null) {
65 4
            $this->buildFromData($triedToDecodeData);
66
        } else {
67 2
            throw new \InvalidArgumentException(
68 2
                'DTO can be built from array|object|json, "'.gettype($data).'" given. Probably tried to pass invalid JSON.'
69
            );
70
        }
71
    }
72
}