Test Failed
Push — master ( 3e9160...6b5548 )
by Jairo
02:47
created

HttpFieldCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 68
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A checkKeyExists() 0 6 2
A fromHttpFieldArray() 0 4 1
A add() 0 4 1
A delete() 0 5 1
A get() 0 5 1
1
<?php
2
/**
3
 * Author: jairo.rodriguez <[email protected]>
4
 */
5
6
namespace BFunky\HttpParser\Entity;
7
8
9
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection;
10
11
class HttpFieldCollection
12
{
13
    /**
14
     * @var HttpField[]
15
     */
16
    protected $httpFields;
17
18
    /**
19
     * HttpFieldCollection constructor.
20
     * @param HttpField[] $httpFields
21
     */
22 5
    public function __construct(array $httpFields = [])
23
    {
24 5
        $this->httpFields = [];
25 5
        foreach ($httpFields as $httpField) {
26 1
            $this->httpFields[$httpField->getName()] = $httpField;
27
        }
28 5
    }
29
30
    /**
31
     * @param HttpField $obj
32
     */
33
    public function add(HttpField $obj): void
34
    {
35
        $this->httpFields[$obj->getName()] = $obj;
36
    }
37
38
    /**
39
     * @param string $key
40
     * @throws HttpFieldNotFoundOnCollection
41
     */
42 2
    public function delete(string $key): void
43
    {
44 2
        $this->checkKeyExists($key);
45
        unset($this->httpFields[$key]);
46
    }
47
48
    /**
49
     * @param string $key
50
     * @return HttpField
51
     * @throws HttpFieldNotFoundOnCollection
52
     */
53 1
    public function get(string $key): HttpField
54
    {
55 1
        $this->checkKeyExists($key);
56
        return $this->httpFields[$key];
57
    }
58
59
    /**
60
     * @param $key
61
     * @throws HttpFieldNotFoundOnCollection
62
     */
63 3
    private function checkKeyExists($key): void
64
    {
65 3
        if (!array_key_exists($key, $this->httpFields)) {
66 2
            throw new  HttpFieldNotFoundOnCollection('Field ' . $key . ' not found');
67
        }
68 1
    }
69
70
    /**
71
     * @param array $httpFields
72
     * @return self
73
     */
74 2
    public static function fromHttpFieldArray(array $httpFields): self
75
    {
76 2
        return new self($httpFields);
77
    }
78
}