HashBasedGroups::distribute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Asdis\Domain\Model\DistributionAlgorithm;
3
4
use Aoe\Asdis\Domain\Model\Asset;
5
use Aoe\Asdis\Domain\Model\Asset\Collection as AssetCollection;
6
use Aoe\Asdis\Domain\Model\DistributionAlgorithm\DistributionAlgorithmInterface;
7
use Aoe\Asdis\Domain\Model\Server\Collection as ServerCollection;
8
9
/**
10
 * A distribution algorithm which is based on the assets hashed filenames.
11
 */
12
class HashBasedGroups implements DistributionAlgorithmInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    const UNKNOWN_GROUP_KEY = 'unknown';
18
19
    /**
20
     * @var \Aoe\Asdis\Domain\Model\Server\Collection
21
     */
22
    private $servers;
23
24
    /**
25
     * @var string
26
     */
27
    private $characters = '0123456789abcdef';
28
29
    /**
30
     * @var array
31
     */
32
    private $groups;
33
34
    /**
35
     * Distributes the given assets to the given servers.
36
     *
37
     * @param \Aoe\Asdis\Domain\Model\Asset\Collection $assets
38
     * @param \Aoe\Asdis\Domain\Model\Server\Collection $servers
39
     * @return void
40
     */
41 1
    public function distribute(AssetCollection $assets, ServerCollection $servers)
42
    {
43 1
        if ($servers->count() < 1) {
44
            return;
45
        }
46 1
        $this->groups  = [];
47 1
        $this->servers = $servers;
48 1
        $this->buildGroups();
49 1
        foreach ($assets as $asset) {
50
            /** @var \Aoe\Asdis\Domain\Model\Asset $asset */
51 1
            $asset->setServer($this->groups[$this->getGroupCharacter($asset)]);
52
        }
53 1
    }
54
55
    /**
56
     * @return \Aoe\Asdis\Domain\Model\Server
57
     */
58 1
    private function getNextServer()
59
    {
60 1
        $server = $this->servers->current();
61 1
        $this->servers->next();
62 1
        if (FALSE === $this->servers->valid()) {
63 1
            $this->servers->rewind();
64
        }
65 1
        return $server;
66
    }
67
68
    /**
69
     * @return void
70
     */
71 1
    private function buildGroups()
72
    {
73 1
        $serverCount = $this->servers->count();
0 ignored issues
show
Unused Code introduced by
The assignment to $serverCount is dead and can be removed.
Loading history...
74 1
        $charCount   = strlen($this->characters);
75 1
        for($i = 0; $i < $charCount; $i++) {
76 1
            $this->groups[$this->characters[$i]] = $this->getNextServer();
77
        }
78 1
        $this->groups[self::UNKNOWN_GROUP_KEY] = $this->getNextServer();
79 1
    }
80
81
    /**
82
     * @param \Aoe\Asdis\Domain\Model\Asset $asset
83
     * @return string
84
     */
85 1
    private function getGroupCharacter(Asset $asset)
86
    {
87 1
        $hash = md5(sha1($asset->getNormalizedPath()));
88 1
        $character = $hash[strlen($hash) - 3];
89 1
        if (false === strpos($this->characters, $character)) {
90
            return self::UNKNOWN_GROUP_KEY;
91
        }
92 1
        return $character;
93
    }
94
}