Headers::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Annotation;
10
11
use RuntimeException;
12
use Tebru;
13
use Tebru\AnnotationReader\AbstractAnnotation;
14
15
/**
16
 * Adds headers statically supplied in the value.
17
 *
18
 *     @Headers("Cache-Control: max-age=640000")
19
 *     @Headers({
20
 *         "X-Foo: Bar",
21
 *         "X-Ping: Pong"
22
 *     })
23
 *
24
 * @author Nate Brunette <[email protected]>
25
 *
26
 * @Annotation
27
 * @Target({"CLASS", "METHOD"})
28
 */
29
class Headers extends AbstractAnnotation
30
{
31
    /**
32
     * Initialize annotation data
33
     *
34
     * @throws \RuntimeException
35
     */
36 5
    protected function init(): void
37
    {
38
        // loop through each string and break on ':'
39 5
        foreach ((array)$this->getValue() as $header) {
40 5
            $position = strpos($header, ':');
41
42 5
            if ($position === false) {
43 1
                throw new RuntimeException('Retrofit: Header in an incorrect format.  Expected "Name: value"');
44
            }
45
46 4
            $name = trim(substr($header, 0, $position));
47 4
            $value = trim(substr($header, $position + 1));
48
49 4
            $this->value[$name] = $value;
50
        }
51 4
    }
52
}
53