FreezableCollectionTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 58
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getItems() 0 1 ?
A ensureItemsAreTraversable() 0 8 3
A performFreeze() 0 16 3
A performUnfreeze() 0 16 3
1
<?php
2
3
namespace Clippings\Freezable;
4
5
/**
6
 * @author    Haralan Dobrev <[email protected]>
7
 * @copyright 2014, Clippings Ltd.
8
 * @license   http://spdx.org/licenses/BSD-3-Clause
9
 */
10
trait FreezableCollectionTrait
11
{
12
    use FreezableTrait;
13
14 6
    private static function ensureItemsAreTraversable($items)
15
    {
16 6
        if (! is_array($items) and ! $items instanceof \Traversable) {
17 2
            throw new \UnexpectedValueException(
18 2
                'Collection returned from getItems() must be either an array or a Traversable object.'
19
            );
20
        }
21 4
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 4
    public function performFreeze()
27
    {
28 4
        $items = $this->getItems();
29
30 4
        self::ensureItemsAreTraversable($items);
31
32 3
        foreach ($items as $item) {
33 3
            if (! $item instanceof FreezableInterface) {
34 1
                throw new \UnexpectedValueException(
35 1
                    'Item must be instance of Clippings\Freezable\FreezableInterface to be freezed'
36
                );
37
            }
38
39 2
            $item->freeze();
40
        }
41 2
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 4
    public function performUnfreeze()
47
    {
48 4
        $items = $this->getItems();
49
50 4
        self::ensureItemsAreTraversable($items);
51
52 3
        foreach ($items as $item) {
53 3
            if (! $item instanceof FreezableInterface) {
54 1
                throw new \UnexpectedValueException(
55 1
                    'Item must be instance of Clippings\Freezable\FreezableInterface to be unfreezed'
56
                );
57
            }
58
59 2
            $item->unfreeze();
60
        }
61 2
    }
62
63
    /**
64
     * @return FreezableInterface[]|Traversable array or traversable of FreezableInterface objects
65
     */
66
    abstract public function getItems();
67
}
68