Passed
Pull Request — master (#838)
by Georges
03:44 queued 01:36
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
declare(strict_types=1);
14
15
namespace Phpfastcache\Config;
16
17
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
18
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
19
20
abstract class AbstractConfigurationOption implements LockableConfigurationInterface
21
{
22
    private bool $lockedObject = false;
23
24
    private ExtendedCacheItemPoolInterface $locker;
25
26
    public function lock(ExtendedCacheItemPoolInterface $poolInstance): static
27
    {
28
        $this->lockedObject = true;
29
        $this->locker = $poolInstance;
30
31
        return $this;
32
    }
33
34
    public function lockedBy(): ExtendedCacheItemPoolInterface
35
    {
36
        return $this->locker;
37
    }
38
39
    public function isLocked(): bool
40
    {
41
        return $this->lockedObject;
42
    }
43
44
    /**
45
     * @throws PhpfastcacheLogicException
46
     */
47
    protected function enforceLockedProperty(string $method): void
48
    {
49
        if ($this->lockedObject === true) {
50
            $dbt = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2);
51
            $cause = $dbt[\array_key_last($dbt)] ?? null;
52
            if ($cause) {
53
                $moreInfo = \sprintf('Caused line %d in %s', $cause['line'], $cause['file']);
54
            }
55
            throw new PhpfastcacheLogicException(
56
                \sprintf(
57
                    'You can no longer change the configuration "%s" as the cache pool instance is now running. %s',
58
                    \lcfirst(\substr($method, 3)),
59
                    $moreInfo ?? ''
60
                )
61
            );
62
        }
63
    }
64
}
65