MultiServerResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolve() 0 10 1
A getNameServerSelectFunction() 0 9 3
A getNameServerRandom() 0 4 1
A getNameServerRoundRobin() 0 6 1
1
<?php
2
3
namespace Thruster\Component\Dns;
4
5
use Thruster\Component\EventLoop\EventLoopInterface;
6
use Thruster\Component\Promise\PromiseInterface;
7
8
/**
9
 * Class MultiServerResolver
10
 *
11
 * @package Thruster\Component\Dns
12
 * @author  Aurimas Niekis <[email protected]>
13
 */
14
class MultiServerResolver extends Resolver
15
{
16
    const STRATEGY_RANDOM = 1;
17
    const STRATEGY_ROUND_ROBIN = 2;
18
19
    /**
20
     * @var string[]
21
     */
22
    private $nameServers;
23
24
    /**
25
     * @var int
26
     */
27
    private $picker;
28
29
    /**
30
     * @var int
31
     */
32
    private $current = 0;
33
34
    public function __construct(array $nameServers, ExecutorInterface $executor, int $strategy = self::STRATEGY_RANDOM)
35
    {
36
        $this->nameServers = $nameServers;
37
        $this->picker      = $this->getNameServerSelectFunction($strategy);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getNameServerSelectFunction($strategy) can also be of type string. However, the property $picker is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
39
        parent::__construct('', $executor);
40
    }
41
42
    public function resolve(string $domain) : PromiseInterface
43
    {
44
        $query = new Query($domain, Message::TYPE_A, Message::CLASS_IN, time());
45
46
        $nameServer = $this->{$this->picker}();
47
48
        var_dump($nameServer);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($nameServer); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
49
50
        return $this->resolveInternal($nameServer, $query);
51
    }
52
53
    private function getNameServerSelectFunction(int $strategy)
54
    {
55
        switch ($strategy) {
56
            case self::STRATEGY_RANDOM:
57
                return 'getNameServerRandom';
58
            case self::STRATEGY_ROUND_ROBIN:
59
                return 'getNameServerRoundRobin';
60
        }
61
    }
62
63
    private function getNameServerRandom() : string
64
    {
65
        return $this->nameServers[array_rand($this->nameServers)];
66
    }
67
68
    private function getNameServerRoundRobin() : string
69
    {
70
        $this->current += 1;
71
72
        return $this->nameServers[$this->current % count($this->nameServers)];
73
    }
74
}
75