Passed
Branch issue-4 (9d1220)
by Andrejs
02:08
created

Imposter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
dl 0
loc 169
ccs 42
cts 48
cp 0.875
rs 10
c 0
b 0
f 0
wmc 20
lcom 3
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequests() 0 4 1
A hasRequests() 0 4 1
A getRequests() 0 4 1
A hasRequestsByCriteria() 0 9 2
A countRequestsByCriteria() 0 8 2
A getPort() 0 4 1
A setPort() 0 4 1
A getProtocol() 0 4 1
A hasName() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A hasPort() 0 4 1
A __construct() 0 8 2
A findRequests() 0 13 4
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 18
    public function __construct($port = null, array $requests = [])
40
    {
41 18
        if (null !== $port) {
42 5
            $this->setPort($port);
43 5
        }
44
45 18
        $this->setRequests($requests);
46 18
    }
47
48
    /**
49
     * @param array $requests
50
     */
51 18
    private function setRequests(array $requests)
52
    {
53 18
        $this->requests = $requests;
54 18
    }
55
56
    /**
57
     * @return bool
58
     */
59 6
    public function hasRequests()
60
    {
61 6
        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 $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 $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 $match
103
     * @return array
104
     * @throws NotFoundException
105
     */
106 1
    public function findRequests($match)
107
    {
108 1
        $matched_requests = [];
109 1
        foreach ($this->requests as $request) {
110 1
            if (\Meare\Juggler\is_subarray_assoc($match, $request)) {
111 1
                $matched_requests[] = $request;
112 1
            }
113 1
        }
114 1
        if (sizeof($matched_requests) > 0) {
115 1
            return $matched_requests;
116
        }
117
        throw new NotFoundException('Unable to find any requests');
118
    }
119
120
    /**
121
     * @return int|null
122
     */
123 9
    public function getPort()
124
    {
125 9
        return $this->port;
126
    }
127
128
    /**
129
     * @param int $port
130
     * @return self
131
     */
132 6
    public function setPort($port)
133
    {
134 6
        $this->port = $port;
135 6
    }
136
137
    /**
138
     * @return string
139
     */
140 8
    public function getProtocol()
141
    {
142 8
        return $this->protocol;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148 1
    public function hasName()
149
    {
150 1
        return null !== $this->getName();
151
    }
152
153
    /**
154
     * @return string|null
155
     */
156 8
    public function getName()
157
    {
158 8
        return $this->name;
159
    }
160
161
    /**
162
     * @param string $name
163
     */
164 4
    public function setName($name)
165
    {
166 4
        $this->name = $name;
167 4
    }
168
169
    /**
170
     * @return bool
171
     */
172 1
    public function hasPort()
173
    {
174 1
        return null !== $this->port;
175
    }
176
}
177