HasLock   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureNotLocked() 0 4 2
A lockItems() 0 4 2
A lock() 0 6 1
A isLocked() 0 3 1
A afterLock() 0 2 1
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