Passed
Pull Request — master (#16)
by
unknown
03:22
created

HashBasedGroups::getNextServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Aoe\Asdis\Domain\Model\DistributionAlgorithm;
4
5
use Aoe\Asdis\Domain\Model\Asset;
6
use Aoe\Asdis\Domain\Model\Asset\Collection as AssetCollection;
7
use Aoe\Asdis\Domain\Model\Server;
8
use Aoe\Asdis\Domain\Model\Server\Collection as ServerCollection;
9
10
/**
11
 * A distribution algorithm which is based on the assets hashed filenames.
12
 */
13
class HashBasedGroups implements DistributionAlgorithmInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    public const UNKNOWN_GROUP_KEY = 'unknown';
19
20
    private ?ServerCollection $servers = null;
21
22
    private string $characters = '0123456789abcdef';
23
24
    private array $groups = [];
25
26
    /**
27
     * Distributes the given assets to the given servers.
28
     */
29 1
    public function distribute(AssetCollection $assets, ServerCollection $servers): void
30
    {
31 1
        if ($servers->count() < 1) {
32
            return;
33
        }
34 1
        $this->groups = [];
35 1
        $this->servers = $servers;
36 1
        $this->buildGroups();
37 1
        foreach ($assets as $asset) {
38
            /** @var Asset $asset */
39 1
            $asset->setServer($this->groups[$this->getGroupCharacter($asset)]);
40
        }
41 1
    }
42
43 1
    private function getNextServer(): Server
44
    {
45 1
        $server = $this->servers->current();
0 ignored issues
show
Bug introduced by
The method current() does not exist on null. ( Ignorable by Annotation )

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

45
        /** @scrutinizer ignore-call */ 
46
        $server = $this->servers->current();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46 1
        $this->servers->next();
47 1
        if (!$this->servers->valid()) {
48 1
            $this->servers->rewind();
49
        }
50 1
        return $server;
51
    }
52
53 1
    private function buildGroups(): void
54
    {
55 1
        $charCount = strlen($this->characters);
56 1
        for ($i = 0; $i < $charCount; $i++) {
57 1
            $this->groups[$this->characters[$i]] = $this->getNextServer();
58
        }
59 1
        $this->groups[self::UNKNOWN_GROUP_KEY] = $this->getNextServer();
60 1
    }
61
62 1
    private function getGroupCharacter(Asset $asset): string
63
    {
64 1
        $hash = md5(sha1($asset->getNormalizedPath()));
0 ignored issues
show
Bug introduced by
It seems like $asset->getNormalizedPath() can also be of type null; however, parameter $string of sha1() does only seem to accept string, 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

64
        $hash = md5(sha1(/** @scrutinizer ignore-type */ $asset->getNormalizedPath()));
Loading history...
65 1
        $character = $hash[strlen($hash) - 3];
66 1
        if (!str_contains($this->characters, $character)) {
67
            return self::UNKNOWN_GROUP_KEY;
68
        }
69 1
        return $character;
70
    }
71
}
72