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

EncodedMap   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getKey() 0 13 3
A getEncodedMapPath() 0 8 1
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