| Total Complexity | 10 |
| Total Lines | 96 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 |
||
| 117 | } |
||
| 118 | } |
||
| 119 |