LockManager::getLock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Lock\Symfony;
16
17
use Acme\App\Core\Port\Lock\LockAlreadyExistsException;
18
use Acme\App\Core\Port\Lock\LockManagerInterface;
19
use Acme\App\Core\Port\Lock\LockNotFoundException;
20
use Symfony\Component\Lock\Factory;
21
use Symfony\Component\Lock\Lock;
22
use Symfony\Component\Lock\Store\FlockStore;
23
24
final class LockManager implements LockManagerInterface
25
{
26
    /**
27
     * @var Lock[]
28
     */
29
    private $lockList = [];
30
31
    /**
32
     * @var Factory
33
     */
34
    private $factory;
35
36
    public function __construct()
37
    {
38
        $this->factory = new Factory(new FlockStore(sys_get_temp_dir()));
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Lock\Factory has been deprecated with message: "Symfony\Component\Lock\Factory" is deprecated since Symfony 4.4 and will be removed in 5.0 use "Symfony\Component\Lock\LockFactory" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
39
    }
40
41
    public function acquire(string $resourceName): void
42
    {
43
        $lock = $this->hasLock($resourceName)
44
            ? $this->getLock($resourceName)
45
            : $this->createLock($resourceName);
46
        $lock->acquire(true);
47
    }
48
49
    public function releaseAll(): void
50
    {
51
        foreach ($this->lockList as $resourceName => $lock) {
52
            $lock->release();
53
        }
54
    }
55
56
    private function createLock(string $resourceName): Lock
57
    {
58
        if (!$this->hasLock($resourceName)) {
59
            $this->storeLock($resourceName, $this->factory->createLock($resourceName));
60
        }
61
62
        return $this->getLock($resourceName);
63
    }
64
65
    private function storeLock(string $resourceName, Lock $lock): void
66
    {
67
        if ($this->hasLock($resourceName)) {
68
            throw new LockAlreadyExistsException();
69
        }
70
        $this->lockList[$resourceName] = $lock;
71
    }
72
73
    private function getLock(string $resourceName): Lock
74
    {
75
        if (!$this->hasLock($resourceName)) {
76
            throw new LockNotFoundException();
77
        }
78
79
        return $this->lockList[$resourceName];
80
    }
81
82
    private function hasLock(string $resourceName): bool
83
    {
84
        return isset($this->lockList[$resourceName]);
85
    }
86
}
87