Completed
Push — master ( e24a08...649c71 )
by Joshua
9s
created

HasOne::calculateChangeSet()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 19
loc 19
rs 8.8571
cc 5
eloc 13
nc 8
nop 0
1
<?php
2
3
namespace As3\Modlr\Models\Embeds;
4
5
use As3\Modlr\Models\Attributes;
6
use As3\Modlr\Models\Embed;
7
8
/**
9
 * Represents the has-one embeds of a Model.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class HasOne extends Attributes
14
{
15
    /**
16
     * Constructor.
17
     *
18
     * @param   Embed[]   $original   Any original properties to apply.
19
     */
20
    public function __construct(array $original = [])
21
    {
22 View Code Duplication
        foreach ($original as $key => $embed) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
23
            if (!$embed instanceof Embed) {
24
                continue;
25
            }
26
            $this->current[$key] = clone $embed;
27
        }
28
        parent::__construct($original);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function areDirty()
35
    {
36
        if (!empty($this->remove)) {
37
            return true;
38
        }
39
40
        foreach ($this->getCurrentAll() as $current) {
41
            if (true === $current->isDirty()) {
42
                return true;
43
            }
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function calculateChangeSet()
0 ignored issues
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...
52
    {
53
        $set = [];
54
        foreach ($this->current as $key => $current) {
55
            if (false === $current->isDirty()) {
56
                continue;
57
            }
58
            $original = isset($this->original[$key]) ? $this->original[$key] : null;
59
            $set[$key]['old'] = $original;
60
            $set[$key]['new'] = $current;
61
        }
62
63
        foreach ($this->remove as $key) {
64
            $set[$key]['old'] = $this->original[$key];
65
            $set[$key]['new'] = null;
66
        }
67
        ksort($set);
68
        return $set;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function rollback()
75
    {
76
        $this->current = [];
77 View Code Duplication
        foreach ($this->original as $key => $embed) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
            if (!$embed instanceof Embed) {
79
                continue;
80
            }
81
            $this->current[$key] = clone $embed;
82
        }
83
        $this->remove = [];
84
        return $this;
85
    }
86
}
87