Passed
Push — master ( a8ea98...ccb0bf )
by SignpostMarv
03:21
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
        static::ThrowIfNotType($object, DaftNestedWriteableObject::class, 1, __METHOD__);
16
17 112
        if ($object instanceof DaftNestedWriteableObject) {
18 112
            $this->RememberDaftObjectWriteableTyped($object);
19
        } else {
20
            parent::RememberDaftObject($object);
21
        }
22 112
    }
23
24
    /**
25
    * @return array<int, DaftNestedObject>
26
    */
27
    abstract public function RecallDaftNestedObjectFullTree(
28
        int $relativeDepthLimit = null
29
    ) : array;
30
31
    abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int;
32
33 112
    protected function RememberDaftObjectWriteableTyped(DaftNestedWriteableObject $object) : void
34
    {
35 112
        $left = $object->GetIntNestedLeft();
36 112
        $right = $object->GetIntNestedRight();
37 112
        $level = $object->GetIntNestedLevel();
38
39 112
        if (0 === $left && 0 === $right && 0 === $level) {
40 110
            if ($this->CountDaftNestedObjectFullTree() > 0) {
41 84
                $tree = $this->RecallDaftNestedObjectFullTree();
42
43
                /**
44
                * @var DaftNestedWriteableObject $end
45
                */
46 84
                $end = end($tree);
47
48 84
                $left = $end->GetIntNestedRight() + 1;
49
            } else {
50 110
                $left = $this->CountDaftNestedObjectFullTree() * 2;
51
            }
52
53 110
            $object->SetIntNestedLeft($left);
54 110
            $object->SetIntNestedRight($left + 1);
55
        }
56
57 112
        parent::RememberDaftObject($object);
58 112
    }
59
60
    /**
61
    * @param DaftObject|string $object
62
    */
63
    protected static function ThrowIfNotType(
64
        $object,
65
        string $type,
66
        int $argument,
67
        string $function
68
    ) : void {
69
        if ( ! is_a($object, DaftNestedWriteableObject::class, is_string($object))) {
70
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
71
                $argument,
72
                static::class,
73
                $function,
74
                DaftNestedWriteableObject::class,
75
                is_string($object) ? $object : get_class($object)
76
            );
77
        }
78
79
        parent::ThrowIfNotType($object, $type, $argument, $function);
80
    }
81
}
82