Passed
Push — master ( 4158cc...85b774 )
by Nico
17:22 queued 07:50
created

EncodedMap::getEncodedMapPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Map;
6
7
use RuntimeException;
8
use Stu\Module\Config\StuConfigInterface;
9
use Stu\Orm\Entity\LayerInterface;
10
11
class EncodedMap implements EncodedMapInterface
12
{
13
    private StuConfigInterface $stuConfig;
14
15
    private ?string $key = null;
16
17 2
    public function __construct(StuConfigInterface $stuConfig)
18
    {
19 2
        $this->stuConfig = $stuConfig;
20
    }
21
22 2
    public function getEncodedMapPath(int $mapFieldType, LayerInterface $layer): string
23
    {
24 2
        $key = $this->getKey();
25
26 1
        return sprintf(
27 1
            '%d/encoded/%s.png',
28 1
            $layer->getId(),
29 1
            implode("/", str_split(bin2hex(base64_encode(crypt((string)$mapFieldType, $key))), 8))
0 ignored issues
show
Bug introduced by
It seems like str_split(bin2hex(base64...pFieldType, $key))), 8) can also be of type true; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

29
            implode("/", /** @scrutinizer ignore-type */ str_split(bin2hex(base64_encode(crypt((string)$mapFieldType, $key))), 8))
Loading history...
30 1
        );
31
    }
32
33 2
    private function getKey(): string
34
    {
35 2
        if ($this->key === null) {
36 2
            $key = $this->stuConfig->getGameSettings()->getMapSettings()->getEncryptionKey();
37
38 2
            if ($key === null) {
39 1
                throw new RuntimeException('encoding key is missing in configuration');
40
            }
41
42 1
            $this->key = $key;
43
        }
44
45 1
        return $this->key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->key could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
}
48