Passed
Push — master ( ccb0bf...cfd82a )
by SignpostMarv
03:08
created

TraitRememberDaftObject::ThrowIfNotType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 4
dl 0
loc 17
ccs 0
cts 9
cp 0
crap 12
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 112
    public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void
14
    {
15 112
        if ($object instanceof DaftNestedWriteableObject) {
16 112
            $this->RememberDaftObjectWriteableTyped($object);
17
        } else {
18
            static::ThrowIfNotType($object, DaftNestedWriteableObject::class, 1, __METHOD__);
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
    protected static function ThrowIfNotType(
62
        $object,
63
        string $type,
64
        int $argument,
65
        string $function
66
    ) : void {
67
        if ( ! is_a($object, DaftNestedWriteableObject::class, is_string($object))) {
68
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
69
                $argument,
70
                static::class,
71
                $function,
72
                DaftNestedWriteableObject::class,
73
                is_string($object) ? $object : get_class($object)
74
            );
75
        }
76
77
        parent::ThrowIfNotType($object, $type, $argument, $function);
78
    }
79
}
80