Passed
Push — v9 ( e6f9d3...0a233b )
by Georges
02:05
created

AbstractConfigurationOption::lockedBy()   A

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
2
/**
3
 *
4
 * This file is part of Phpfastcache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Config;
17
18
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
19
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
20
21
abstract class AbstractConfigurationOption implements LockableConfigurationInterface
22
{
23
    private bool $lockedObject = false;
24
25
    private ExtendedCacheItemPoolInterface $locker;
26
27
    public function lock(ExtendedCacheItemPoolInterface $poolInstance): static
28
    {
29
        $this->lockedObject = true;
30
        $this->locker = $poolInstance;
31
32
        return $this;
33
    }
34
35
    public function lockedBy(): ExtendedCacheItemPoolInterface
36
    {
37
        return $this->locker;
38
    }
39
40
    public function isLocked(): bool
41
    {
42
        return $this->lockedObject;
43
    }
44
45
    /**
46
     * @throws PhpfastcacheLogicException
47
     */
48
    protected function enforceLockedProperty(string $method): void
49
    {
50
        if ($this->lockedObject === true) {
51
            $dbt = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2);
52
            $cause = $dbt[\array_key_last($dbt)] ?? null;
53
            if ($cause) {
54
                $moreInfo = \sprintf('Caused line %d in %s', $cause['line'], $cause['file']);
55
            }
56
            throw new PhpfastcacheLogicException(
57
                \sprintf(
58
                    'You can no longer change the configuration "%s" as the cache pool instance is now running. %s',
59
                    \lcfirst(\substr($method, 3)),
60
                    $moreInfo ?? ''
61
                )
62
            );
63
        }
64
    }
65
}
66