Passed
Push — master ( c4f9c2...b880d1 )
by SignpostMarv
02:45
created

TraitRememberDaftObject::ThrowIfNotType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 4
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
trait TraitRememberDaftObject
12
{
13 114
    public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void
14
    {
15 114
        if ($object instanceof DaftNestedWriteableObject) {
16 112
            $this->RememberDaftObjectWriteableTyped($object);
17
        } else {
18 2
            static::ThrowIfNotType($object, DaftNestedWriteableObject::class, 1, __FUNCTION__);
19
        }
20 112
    }
21
22
    /**
23
    * @return array<int, DaftNestedObject>
24
    */
25
    abstract public function RecallDaftNestedObjectFullTree(
26
        int $relativeDepthLimit = null
27
    ) : array;
28
29
    abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int;
30
31 112
    protected function RememberDaftObjectWriteableTyped(DaftNestedWriteableObject $object) : void
32
    {
33 112
        $left = $object->GetIntNestedLeft();
34 112
        $right = $object->GetIntNestedRight();
35 112
        $level = $object->GetIntNestedLevel();
36
37 112
        if (0 === $left && 0 === $right && 0 === $level) {
38 110
            if ($this->CountDaftNestedObjectFullTree() > 0) {
39 84
                $tree = $this->RecallDaftNestedObjectFullTree();
40
41
                /**
42
                * @var DaftNestedWriteableObject $end
43
                */
44 84
                $end = end($tree);
45
46 84
                $left = $end->GetIntNestedRight() + 1;
47
            } else {
48 110
                $left = $this->CountDaftNestedObjectFullTree() * 2;
49
            }
50
51 110
            $object->SetIntNestedLeft($left);
52 110
            $object->SetIntNestedRight($left + 1);
53
        }
54
55 112
        parent::RememberDaftObject($object);
56 112
    }
57
58
    /**
59
    * @param DaftObject|string $object
60
    */
61 2
    protected static function ThrowIfNotType(
62
        $object,
63
        string $type,
64
        int $argument,
65
        string $function
66
    ) : void {
67 2
        if ( ! is_a($object, DaftNestedWriteableObject::class, is_string($object))) {
68 2
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
69 2
                $argument,
70 2
                static::class,
71 2
                $function,
72 2
                DaftNestedWriteableObject::class,
73 2
                is_string($object) ? $object : get_class($object)
74
            );
75
        }
76
77
        parent::ThrowIfNotType($object, $type, $argument, $function);
78
    }
79
}
80