Completed
Push — 4.0 ( 4d6b64...872671 )
by Marco
03:00
created

HeadersTrait::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.7085

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.7085
1
<?php namespace Comodojo\Dispatcher\Traits;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
trait HeadersTrait {
26
27
    protected $headers = [];
28
29 3
    public function get($header = null) {
30
31 3 View Code Duplication
        if ( is_null($header) ) return $this->headers;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
33 3
        else if ( array_key_exists($header, $this->headers) ) return $this->headers[$header];
34
35 2
        else return null;
36
37
    }
38
39 1
    public function getAsString($header = null) {
40
41 1
        if ( is_null($header) ) {
42
43
            return array_map( [$this, 'headerToString'],
44
                array_keys($this->headers),
45
                array_values($this->headers)
46
            );
47
48 1 View Code Duplication
        } else if ( array_key_exists($header, $this->headers) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
50 1
            return self::headerToString($header, $this->headers[$header]);
51
52
        } else return null;
53
54
    }
55
56 2
    public function set($header, $value=null) {
57
58 2
        if ( is_null($value) ) {
59
60
            $header = explode(":", $header, 2);
61
62
            $this->headers[$header[0]] = isset($header[1]) ? $header[1] : '';
63
64
        } else {
65
66 2
            $this->headers[$header] = $value;
67
68
        }
69
70 2
        return $this;
71
72
    }
73
74 2
    public function delete($header = null) {
75
76 2
        if ( is_null($header) ) {
77
78 1
            $this->headers = array();
79
80 1
            return true;
81
82 2 View Code Duplication
        } else if ( array_key_exists($header, $this->headers) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
84 1
            unset($this->headers[$header]);
85
86 1
            return true;
87
88
        } else {
89
90 1
            return false;
91
92
        }
93
94
    }
95
96 1
    public function merge($headers) {
97
98 1
        foreach ($headers as $key => $value) {
99
            $this->set($key, $value);
100 1
        }
101
102 1
        return $this;
103
104
    }
105
106 1
    private static function headerToString($header, $value) {
107
108 1
        return (string)($header.':'.$value);
109
110
    }
111
112
}
113