RoundRobin::getNextServer()   A
last analyzed

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 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
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\Collection as AssetCollection;
5
use Aoe\Asdis\Domain\Model\DistributionAlgorithm\DistributionAlgorithmInterface;
6
use Aoe\Asdis\Domain\Model\Server\Collection as ServerCollection;
7
8
/**
9
 * RoundRobin implementation of a distribution algorithm.
10
 */
11
class RoundRobin implements DistributionAlgorithmInterface
12
{
13
    /**
14
     * @var \Aoe\Asdis\Domain\Model\Server\Collection
15
     */
16
    private $servers;
17
18
    /**
19
     * Distributes the given assets to the given servers.
20
     *
21
     * @param \Aoe\Asdis\Domain\Model\Asset\Collection $assets
22
     * @param \Aoe\Asdis\Domain\Model\Server\Collection $servers
23
     * @return void
24
     */
25 1
    public function distribute(AssetCollection $assets, ServerCollection $servers)
26
    {
27 1
        if ($servers->count() < 1) {
28
            return;
29
        }
30 1
        $this->servers = $servers;
31 1
        foreach($assets as $asset) {
32
            /** @var \Aoe\Asdis\Domain\Model\Asset $asset */
33 1
            $asset->setServer($this->getNextServer());
34
        }
35 1
    }
36
37
    /**
38
     * @return \Aoe\Asdis\Domain\Model\Server
39
     */
40 1
    private function getNextServer()
41
    {
42 1
        $server = $this->servers->current();
43 1
        $this->servers->next();
44 1
        if (false === $this->servers->valid()) {
45 1
            $this->servers->rewind();
46
        }
47 1
        return $server;
48
    }
49
}