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

HasMany::areDirty()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.2
cc 4
eloc 7
nc 4
nop 0
1
<?php
2
3
namespace As3\Modlr\Models\Embeds;
4
5
use As3\Modlr\Models\Attributes;
6
use As3\Modlr\Models\Collections\EmbedCollection;
7
8
/**
9
 * Represents the has-many relationships of a Model.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13 View Code Duplication
class HasMany extends Attributes
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * Extended to also check for dirty states of has-many Collections.
17
     *
18
     * {@inheritDoc}
19
     */
20
    public function areDirty()
21
    {
22
        if (true === parent::areDirty()) {
23
            return true;
24
        }
25
        foreach ($this->getOriginalAll() as $collection) {
26
            if (true === $collection->isDirty()) {
27
                return true;
28
            }
29
        }
30
        return false;
31
    }
32
33
    /**
34
     * Extended to also rollback changes of has-many collections.
35
     *
36
     * {@inheritDoc}
37
     */
38
    public function rollback()
39
    {
40
        parent::rollback();
41
        foreach ($this->getOriginalAll() as $collection) {
42
            if ($collection instanceof EmbedCollection) {
43
                $collection->rollback();
44
            }
45
        }
46
        return $this;
47
    }
48
49
    /**
50
     * Overwritten to account for collection change sets.
51
     *
52
     * {@inheritDoc}
53
     */
54
    public function calculateChangeSet()
55
    {
56
        $set = [];
57
        foreach ($this->getOriginalAll() as $key => $collection) {
58
            if (false === $collection->isDirty()) {
59
                continue;
60
            }
61
            $set[$key] = $collection->calculateChangeSet();
62
        }
63
        return $set;
64
    }
65
}
66