Completed
Push — master ( ab502e...07a33d )
by Zaahid
06:14
created

AbstractHeader::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Header;
8
9
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer;
10
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService;
11
12
/**
13
 * Abstract base class representing a mime email's header.
14
 * 
15
 * The base class sets up the header's consumer, sets the name of the header and
16
 * calls the consumer to parse the header's value.
17
 * 
18
 * AbstractHeader::getConsumer is an abstract method that must be overridden to
19
 * return an appropriate Consumer\AbstractConsumer type.
20
 * 
21
 * AbstractHeader::parseHeaderValue shou
22
 *
23
 * @author Zaahid Bateson
24
 */
25
abstract class AbstractHeader
26
{
27
    /**
28
     * @var string the name of the header
29
     */
30
    protected $name;
31
    
32
    /**
33
     * @var \ZBateson\MailMimeParser\Header\Consumer\Part\HeaderPart[] the
34
     * header's parts (as returned from the consumer)
35
     */
36
    protected $parts;
37
    
38
    /**
39
     * @var string the raw value
40
     */
41
    protected $rawValue;
42
    
43
    /**
44
     * Assigns the header's name and raw value, then calls getConsumer and
45
     * parseHeaderValue to extract a parsed value.
46
     * 
47
     * @param ConsumerService $consumerService
48
     * @param string $name
49
     * @param string $value
50
     */
51 24
    public function __construct(ConsumerService $consumerService, $name, $value)
52
    {
53 24
        $this->name = $name;
54 24
        $this->rawValue = $value;
55
        
56 24
        $consumer = $this->getConsumer($consumerService);
57 24
        $this->setParseHeaderValue($consumer);
58 24
    }
59
    
60
    /**
61
     * Returns the header's Consumer
62
     * 
63
     * @param ConsumerService $consumerService
64
     * @return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer
65
     */
66
    abstract protected function getConsumer(ConsumerService $consumerService);
67
    
68
    /**
69
     * Calls the consumer and assigns the parsed parts to member variables.
70
     * 
71
     * The default implementation assigns the returned value to $this->part.
72
     * 
73
     * @param AbstractConsumer $consumer
74
     */
75 24
    protected function setParseHeaderValue(AbstractConsumer $consumer)
76
    {
77 24
        $this->parts = $consumer($this->rawValue);
78 24
    }
79
80
    /**
81
     * Returns an array of HeaderPart objects associated with this header.
82
     * 
83
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPart[]
84
     */
85 8
    public function getParts()
86
    {
87 8
        return $this->parts;
88
    }
89
    
90
    /**
91
     * Returns the parsed value of the header -- calls getValue on $this->part
92
     * 
93
     * @return string
94
     */
95 11
    public function getValue()
96
    {
97 11
        if (!empty($this->parts)) {
98 10
            return $this->parts[0]->getValue();
99
        }
100 1
        return null;
101
    }
102
    
103
    /**
104
     * Returns the raw value of the header prior to any processing.
105
     * 
106
     * @return string
107
     */
108 1
    public function getRawValue()
109
    {
110 1
        return $this->rawValue;
111
    }
112
    
113
    /**
114
     * Returns the name of the header.
115
     * 
116
     * @return string
117
     */
118 2
    public function getName()
119
    {
120 2
        return $this->name;
121
    }
122
    
123
    /**
124
     * Returns the string representation of the header.  At the moment this is
125
     * just in the form of:
126
     * 
127
     * <HeaderName>: <RawValue>
128
     * 
129
     * No additional processing is performed (for instance to wrap long lines.)
130
     * 
131
     * @return string
132
     */
133 4
    public function __toString()
134
    {
135 4
        return "{$this->name}: {$this->rawValue}";
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
136
    }
137
}
138