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(); |
|
|
|
|
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())); |
|
|
|
|
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
|
|
|
|
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.