Completed
Push — master ( 7f142d...1b2ea5 )
by Vitalijs
02:25
created

DTOBaseBuilder::build()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.2
cc 4
eloc 13
nc 4
nop 1
crap 4
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
    /** @property DTOBase $dto */
14
    private $dto;
15
16 40
    public function __construct(DTOBase $dtoBase)
17
    {
18 40
        $this->dto = $dtoBase;
19 40
    }
20
21
    /**
22
     * Build DTO from given type of data
23
     * @param $data
24
     * @throws \InvalidArgumentException
25
     */
26 40
    public function build($data)
27
    {
28 40
        switch (gettype($data)) {
29 40
            case 'array':
30 31
                $this->buildFromArray($data);
31 31
                break;
32 10
            case 'object':
33 2
                $this->buildFromObject($data);
34 2
                break;
35 8
            case 'string':
36 6
                $this->buildFromJson($data);
37 4
                break;
38
            default:
39 2
                throw new \InvalidArgumentException('DTO can be built from array|object|json, "'.gettype($data).'" given.');
40
        }
41 36
    }
42
43
    /**
44
     * Build DTO from provided data
45
     * @param $array
46
     */
47 31 View Code Duplication
    private function buildFromArray($array)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 31
        foreach ($this->dto->getDefault() as $key => $value) {
50 29
            if (isset($array[$key])) {
51 13
                $this->dto[$key] = $array[$key];
52
            } else {
53 29
                $this->dto[$key] = $value;
54
            }
55
        }
56 31
    }
57
58
    /**
59
     * Build DTO from provided data
60
     * @param $object
61
     */
62 6 View Code Duplication
    private function buildFromObject($object)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 6
        foreach ($this->dto->getDefault() as $key => $value) {
65 6
            if (isset($object->{$key})) {
66 6
                $this->dto[$key] = $object->{$key};
67
            }else {
68 6
                $this->dto[$key] = $value;
69
            }
70
        }
71 6
    }
72
73
    /**
74
     * Try to build from provided string as JSON
75
     * @param string $data
76
     * @throws \InvalidArgumentException
77
     */
78 6
    private function buildFromJson($data)
79
    {
80 6
        $triedToDecodeData = json_decode($data);
81
82 6
        if ($triedToDecodeData !== null) {
83 4
            $this->buildFromObject($triedToDecodeData);
84
        } else {
85 2
            throw new \InvalidArgumentException(
86 2
                'DTO can be built from array|object|json, "'.gettype($data).'" given. Probably tried to pass invalid JSON.'
87
            );
88
        }
89
    }
90
}