Imposter::setName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Meare\Juggler\Imposter;
4
5
6
use Meare\Juggler\Exception\Client\NotFoundException;
7
8
abstract class Imposter implements \JsonSerializable
9
{
10
    const PROTOCOL_HTTPS = 'https';
11
    const PROTOCOL_HTTP = 'http';
12
    const PROTOCOL_TCP = 'tcp';
13
    const PROTOCOL_SMTP = 'smtp';
14
15
    /**
16
     * @var string
17
     */
18
    protected $protocol;
19
20
    /**
21
     * @var array
22
     */
23
    protected $requests;
24
25
    /**
26
     * @var int|null
27
     */
28
    private $port;
29
30
    /**
31
     * @var string
32
     */
33
    private $name;
34
35
    /**
36
     * @param int|null $port
37
     * @param array    $requests
38
     */
39 23
    public function __construct($port = null, array $requests = [])
40
    {
41 23
        if (null !== $port) {
42 5
            $this->setPort($port);
43 5
        }
44
45 23
        $this->setRequests($requests);
46 23
    }
47
48
    /**
49
     * @param array $requests
50
     */
51 23
    private function setRequests(array $requests)
52
    {
53 23
        $this->requests = $requests;
54 23
    }
55
56
    /**
57
     * @return bool
58
     */
59 8
    public function hasRequests()
60
    {
61 8
        return sizeof($this->requests) > 0;
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getRequests()
68
    {
69
        return $this->requests;
70
    }
71
72
    /**
73
     * @param array|callable $criteria
74
     * @param int            $exactly Expect exactly n occurrences
75
     * @return bool
76
     * @throws NotFoundException
77
     */
78 1
    public function hasRequestsByCriteria($criteria, $exactly = null)
79
    {
80 1
        $num = $this->countRequestsByCriteria($criteria);
81 1
        if (null === $exactly) {
82 1
            return $num > 0;
83
        } else {
84
            return $num === $exactly;
85
        }
86
    }
87
88
    /**
89
     * @param array|callable $criteria
90
     * @return int
91
     */
92 1
    public function countRequestsByCriteria($criteria)
93
    {
94
        try {
95 1
            return sizeof($this->findRequests($criteria));
96
        } catch (NotFoundException $e) {
97
            return 0;
98
        }
99
    }
100
101
    /**
102
     * @param array|callable $criteria
103
     * @return array[]
104
     * @throws NotFoundException
105
     * @throws \InvalidArgumentException
106
     */
107 3
    public function findRequests($criteria)
108
    {
109 3
        switch (true) {
110 3
            case is_array($criteria):
111 2
                $matched_requests = $this->findRequestsWithSubarray($criteria);
112 2
                break;
113
114 1
            case is_callable($criteria):
115 1
                $matched_requests = $this->findRequestsWithCallable($criteria);
116 1
                break;
117
118
            default:
119
                throw new \InvalidArgumentException('Criteria could only be array or callable');
120
        }
121
122 3
        if (0 === sizeof($matched_requests)) {
123
            throw new NotFoundException('Unable to find any requests per criteria');
124
        }
125
126 3
        return $matched_requests;
127
    }
128
129
    /**
130
     * @param array $criteria
131
     * @return array
132
     */
133 2
    private function findRequestsWithSubarray($criteria)
134
    {
135 2
        $matched_requests = [];
136 2
        foreach ($this->requests as $request) {
137 2
            if (\Meare\Juggler\is_subarray_assoc($criteria, $request)) {
138 2
                $matched_requests[] = $request;
139 2
            }
140 2
        }
141
142 2
        return $matched_requests;
143
    }
144
145
    /**
146
     * @param callback $callback
147
     * @return array
148
     */
149 1
    private function findRequestsWithCallable($callback)
150
    {
151 1
        $matched_requests = [];
152 1
        foreach ($this->requests as $request) {
153 1
            if (true === $callback($request)) {
154 1
                $matched_requests[] = $request;
155 1
            }
156 1
        }
157
158 1
        return $matched_requests;
159
    }
160
161
    /**
162
     * @return int|null
163
     */
164 11
    public function getPort()
165
    {
166 11
        return $this->port;
167
    }
168
169
    /**
170
     * @param int $port
171
     */
172 6
    public function setPort($port)
173
    {
174 6
        $this->port = $port;
175 6
    }
176
177
    /**
178
     * @return string
179
     */
180 10
    public function getProtocol()
181
    {
182 10
        return $this->protocol;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188 1
    public function hasName()
189
    {
190 1
        return null !== $this->getName();
191
    }
192
193
    /**
194
     * @return string|null
195
     */
196 11
    public function getName()
197
    {
198 11
        return $this->name;
199
    }
200
201
    /**
202
     * @param string $name
203
     */
204 5
    public function setName($name)
205
    {
206 5
        $this->name = $name;
207 5
    }
208
209
    /**
210
     * @return bool
211
     */
212 1
    public function hasPort()
213
    {
214 1
        return null !== $this->port;
215
    }
216
}
217