HasLock::isLocked()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace hiqdev\php\billing\product\trait;
4
5
use hiqdev\php\billing\product\Exception\LockedException;
6
7
trait HasLock
8
{
9
    private bool $locked = false;
10
11
    public function lock(): void
12
    {
13
        // Lock the state to prevent further modifications
14
        $this->locked = true;
15
16
        $this->afterLock();
17
    }
18
19
    protected function ensureNotLocked(): void
20
    {
21
        if ($this->isLocked()) {
22
            throw new LockedException('Modifications are not allowed after the class has been locked.');
23
        }
24
    }
25
26
    protected function isLocked(): bool
27
    {
28
        return $this->locked;
29
    }
30
31
    protected function afterLock(): void
32
    {
33
        // Hook
34
    }
35
36
    /**
37
     * @param HasLockInterface[] $items
38
     * @return void
39
     */
40
    protected function lockItems(array $items): void
41
    {
42
        foreach ($items as $item) {
43
            $item->lock();
44
        }
45
    }
46
}
47