Passed
Push — master ( 3d0840...13d157 )
by Nico
48:12 queued 22:32
created

SemaphoreUtil::acquireMainSemaphore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Control;
4
5
use RuntimeException;
6
use Stu\Component\Game\SemaphoreConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\SemaphoreConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Stu\Exception\SemaphoreException;
8
use Stu\Module\Config\StuConfigInterface;
9
use Stu\Module\Logging\LoggerEnum;
10
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11
use Stu\Module\Logging\LoggerUtilInterface;
12
13
final class SemaphoreUtil implements SemaphoreUtilInterface
14
{
15
    private GameControllerInterface $game;
16
17
    private StuConfigInterface $stuConfig;
18
19
    private LoggerUtilInterface $loggerUtil;
20
21 1
    public function __construct(
22
        GameControllerInterface $game,
23
        StuConfigInterface $stuConfig,
24
        LoggerUtilFactoryInterface $loggerUtilFactory
25
    ) {
26 1
        $this->game = $game;
27 1
        $this->stuConfig = $stuConfig;
28 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31
    public function getSemaphore(int $key)
32
    {
33
        if (!$this->isSemaphoreUsageActive()) {
34
            return;
35
        }
36
37
        $semaphore = sem_get(
38
            $key,
39
            1,
40
            0666,
41
            SemaphoreConstants::AUTO_RELEASE_SEMAPHORES
42
        );
43
44
        if ($semaphore === false) {
45
            throw new RuntimeException('Error getting semaphore');
46
        }
47
48
        return $semaphore;
49
    }
50
51
    public function acquireMainSemaphore($semaphore): void
52
    {
53
        if (!$this->isSemaphoreUsageActive()) {
54
            return;
55
        }
56
57
        $this->acquire($semaphore);
58
    }
59
60
    public function acquireSemaphore(int $key, $semaphore): void
61
    {
62
        if (!$this->isSemaphoreUsageActive()) {
63
            return;
64
        }
65
66
        if ($this->game->isSemaphoreAlreadyAcquired($key)) {
67
            return;
68
        }
69
70
        $this->acquire($semaphore);
71
        $this->game->addSemaphore($key, $semaphore);
72
    }
73
74
    private function acquire($semaphore): void
75
    {
76
        if (!sem_acquire($semaphore)) {
77
            throw new SemaphoreException("Error acquiring Semaphore!");
78
        }
79
    }
80
81
    public function releaseSemaphore($semaphore, bool $doRemove = false): void
82
    {
83
        if (!$this->isSemaphoreUsageActive()) {
84
            return;
85
        }
86
87
        $this->release($semaphore, $doRemove);
88
    }
89
90
    private function release($semaphore, bool $doRemove): void
91
    {
92
        if (!sem_release($semaphore)) {
93
            $this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR);
94
            $this->loggerUtil->log("Error releasing Semaphore!");
95
            return;
96
            //throw new SemaphoreException("Error releasing Semaphore!");
97
        }
98
99
        if ($doRemove && !sem_remove($semaphore)) {
100
            $this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR);
101
            $this->loggerUtil->log("Error removing Semaphore!");
102
            //throw new SemaphoreException("Error removing Semaphore!");
103
        }
104
    }
105
106
    private function isSemaphoreUsageActive(): bool
107
    {
108
        return $this->stuConfig->getGameSettings()->useSemaphores();
109
    }
110
}
111