Passed
Pull Request — master (#1716)
by Nico
23:15
created

LockManager::getLockPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Lock;
6
7
use Stu\Module\Config\StuConfigInterface;
8
9
final class LockManager implements LockManagerInterface
10
{
11
    private StuConfigInterface $config;
12
13 2
    public function __construct(StuConfigInterface $config)
14
    {
15 2
        $this->config = $config;
16
    }
17
18 1
    public function setLock(int $batchGroupId, LockTypeEnum $type): void
19
    {
20 1
        @touch($this->getLockPath($batchGroupId, $type));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for touch(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

20
        /** @scrutinizer ignore-unhandled */ @touch($this->getLockPath($batchGroupId, $type));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
21
    }
22
23 1
    public function clearLock(int $batchGroupId, LockTypeEnum $type): void
24
    {
25 1
        @unlink($this->getLockPath($batchGroupId, $type));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

25
        /** @scrutinizer ignore-unhandled */ @unlink($this->getLockPath($batchGroupId, $type));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
26
    }
27
28 1
    public function isLocked(int $entityId, LockTypeEnum $type): bool
29
    {
30 1
        return @file_exists($this->getLockPath($entityId % $this->getGroupCount($type) + 1, $type));
31
    }
32
33 1
    private function getLockPath(int $batchGroupId, LockTypeEnum $type): string
34
    {
35 1
        return sprintf(
36 1
            '%s/%s_%d.lock',
37 1
            $this->config->getGameSettings()->getTempDir(),
38 1
            $type->getName(),
39 1
            $batchGroupId
40 1
        );
41
    }
42
43 1
    private function getGroupCount(LockTypeEnum $type): int
44
    {
45
        switch ($type) {
46 1
            case LockTypeEnum::COLONY_GROUP:
47 1
                return $this->config->getGameSettings()->getColonySettings()->getTickWorker();
48
            case LockTypeEnum::SHIP_GROUP:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return integer. Consider adding a default case to the switch.
Loading history...
49
                return 1;
50
        }
51
    }
52
}
53