Passed
Push — master ( 6d626d...5de675 )
by Janko
09:20
created

SemaphoreUtil::acquireMainSemaphore()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Control;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
6
use RuntimeException;
7
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...
8
use Stu\Exception\SemaphoreException;
9
use Stu\Module\Config\StuConfigInterface;
10
use Stu\Module\Logging\LoggerEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Logging\LoggerEnum 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...
11
use Stu\Module\Logging\LoggerUtilFactoryInterface;
12
use Stu\Module\Logging\LoggerUtilInterface;
13
use SysvSemaphore;
14
15
final class SemaphoreUtil implements SemaphoreUtilInterface
16
{
17
    private LoggerUtilInterface $loggerUtil;
18
19 1
    public function __construct(
20
        private GameControllerInterface $game,
21
        private StuConfigInterface $stuConfig,
22
        LoggerUtilFactoryInterface $loggerUtilFactory
23
    ) {
24 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
25
    }
26
27 38
    #[Override]
28
    public function acquireSemaphore(int $key): null|int|SysvSemaphore
29
    {
30 38
        if (!$this->isSemaphoreUsageActive()) {
31 38
            return null;
32
        }
33
34
        $semaphore = $this->getSemaphore($key);
35
36
        if ($this->game->isSemaphoreAlreadyAcquired($key)) {
37
            return null;
38
        }
39
40
        $this->acquire($semaphore);
41
        $this->game->addSemaphore($key, $semaphore);
42
43
        return $semaphore;
44
    }
45
46
    private function getSemaphore(int $key): SysvSemaphore
47
    {
48
        $semaphore = sem_get(
49
            $key,
50
            1,
51
            0o666,
52
            SemaphoreConstants::AUTO_RELEASE_SEMAPHORES
53
        );
54
55
        if ($semaphore === false) {
56
            throw new RuntimeException('Error getting semaphore');
57
        }
58
59
        return $semaphore;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $semaphore could return the type resource which is incompatible with the type-hinted return SysvSemaphore. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
62
    private function acquire(SysvSemaphore $semaphore): void
63
    {
64
        if (!sem_acquire($semaphore)) {
65
            throw new SemaphoreException("Error acquiring Semaphore!");
66
        }
67
    }
68
69 38
    #[Override]
70
    public function releaseSemaphore(null|int|SysvSemaphore $semaphore, bool $doRemove = false): void
71
    {
72 38
        if (!$this->isSemaphoreUsageActive() || !$semaphore instanceof SysvSemaphore) {
73 38
            return;
74
        }
75
76
        $this->release($semaphore, $doRemove);
77
    }
78
79
    private function release(SysvSemaphore $semaphore, bool $doRemove): void
80
    {
81
        if (!sem_release($semaphore)) {
82
            $this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR);
83
            $this->loggerUtil->log("Error releasing Semaphore!");
84
            return;
85
            //throw new SemaphoreException("Error releasing Semaphore!");
86
        }
87
88
        if ($doRemove && !sem_remove($semaphore)) {
89
            $this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR);
90
            $this->loggerUtil->log("Error removing Semaphore!");
91
            //throw new SemaphoreException("Error removing Semaphore!");
92
        }
93
    }
94
95 38
    private function isSemaphoreUsageActive(): bool
96
    {
97 38
        return $this->stuConfig->getGameSettings()->useSemaphores();
98
    }
99
}
100