Mirror   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 96
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A __construct() 0 19 5
A getMaster() 0 3 1
A getNext() 0 5 1
A getAll() 0 3 1
A remove() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Packagist Mirror.
7
 *
8
 * For the full license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webs\Mirror;
13
14
use PHPSnippets\DataStructures\CircularArray;
15
16
/**
17
 * Middleware to http operations.
18
 *
19
 * @author Webysther Nunes <[email protected]>
20
 */
21
class Mirror
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $master;
27
28
    /**
29
     * @var array
30
     */
31
    protected $slaves;
32
33
    /**
34
     * @var CircularArray
35
     */
36
    protected $all;
37
38
    /**
39
     * @var array
40
     */
41
    protected $data;
42
43
    /**
44
     * @param string $master
45
     * @param array  $slaves
46
     */
47 2
    public function __construct(string $master, array $slaves)
48
    {
49 2
        $this->master = $master;
50 2
        $this->slaves = $slaves;
51
        $this->data = [];
52 2
53 2
        foreach ($slaves as $value) {
54 2
            if(empty($value) || !filter_var($value, FILTER_VALIDATE_URL)){
55
                continue;
56
            }
57 2
58 2
            $this->data[] = $value;
59
        }
60
61
        if (!in_array($master, $this->data)) {
62
            $this->data[] = $master;
63 2
        }
64
65 2
        $this->all = CircularArray::fromArray($this->data);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getMaster():string
72
    {
73 2
        return $this->master;
74
    }
75 2
76
    /**
77
     * Get all mirrors.
78
     *
79
     * @return CircularArray
80
     */
81
    public function getAll():CircularArray
82
    {
83 2
        return $this->all;
84
    }
85 2
86
    /**
87 2
     * Get next item.
88
     *
89
     * @return string
90
     */
91
    public function getNext():string
92
    {
93
        $this->all->next();
94
95 1
        return $this->all->current();
96
    }
97 1
98 1
    /**
99
     * @param string $value
100 1
     *
101
     * @return CircularArray
102
     */
103
    public function remove(string $value):CircularArray
104
    {
105
        $this->data = array_values(array_diff($this->data, [$value]));
106 1
        $this->all = CircularArray::fromArray($this->data);
107
108 1
        return $this->getAll();
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function toArray():array
115
    {
116
        return $this->getAll()->toArray();
117
    }
118
}
119