Passed
Push — master ( a30145...bc128f )
by Webysther
36s
created

Mirror::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.4285
c 0
b 0
f 0
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 1
    public function __construct(string $master, array $slaves)
48
    {
49 1
        $this->master = $master;
50 1
        $this->slaves = $slaves;
51
52 1
        $this->data = $slaves;
53 1
        if (!in_array($master, $this->data)) {
54
            $this->data[] = $master;
55
        }
56
57 1
        $this->all = CircularArray::fromArray($this->data);
58 1
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getMaster():string
64
    {
65
        return $this->master;
66
    }
67
68
    /**
69
     * Get all mirrors.
70
     *
71
     * @return CircularArray
72
     */
73 1
    public function getAll():CircularArray
74
    {
75 1
        return $this->all;
76
    }
77
78
    /**
79
     * Get next item.
80
     *
81
     * @return string
82
     */
83
    public function getNext():string
84
    {
85
        $this->all->next();
86
87
        return $this->all->current();
88
    }
89
90
    /**
91
     * @param string $value
92
     *
93
     * @return CircularArray
94
     */
95
    public function remove(string $value):CircularArray
96
    {
97
        $this->data = array_values(array_diff($this->data, [$value]));
98
        $this->all = CircularArray::fromArray($this->data);
99
100
        return $this->getAll();
101
    }
102
103
    /**
104
     * @return array
105
     */
106 1
    public function toArray():array
107
    {
108 1
        return $this->getAll()->toArray();
109
    }
110
}
111