Completed
Pull Request — 5.x (#24)
by Lars
06:58
created

Swift_Mime_Headers_IdentificationHeader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 173
ccs 45
cts 48
cp 0.9375
rs 10
wmc 16
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldType() 0 4 1
A setFieldBodyModel() 0 4 1
A getFieldBodyModel() 0 4 1
A getIds() 0 4 1
A __construct() 0 6 1
A setId() 0 8 2
A getId() 0 8 2
A setIds() 0 12 2
A getFieldBody() 0 14 3
A _assertValidId() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * An ID MIME Header for something like Message-ID or Content-ID.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_Headers_IdentificationHeader extends Swift_Mime_Headers_AbstractHeader
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * The IDs used in the value of this Header.
20
     *
21
     * This may hold multiple IDs or just a single ID.
22
     *
23
     * @var string[]
24
     */
25
    private $_ids = array();
26
27
    /**
28
     * @var Swift_EmailValidatorBridge
29
     */
30
    private $_emailValidator;
31
32
    /**
33
     * Creates a new IdentificationHeader with the given $name and $id.
34
     *
35
     * @param string                     $name
36
     * @param Swift_EmailValidatorBridge $emailValidator
37
     */
38 140
    public function __construct($name, Swift_EmailValidatorBridge $emailValidator)
39
    {
40 140
        $this->setFieldName($name);
41 140
        $this->_emailValidator = $emailValidator;
42 140
        parent::__construct();
43 140
    }
44
45
    /**
46
     * Get the type of Header that this instance represents.
47
     *
48
     * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
49
     * @see TYPE_DATE, TYPE_ID, TYPE_PATH
50
     *
51
     * @return int
52
     */
53 1
    public function getFieldType()
54
    {
55 1
        return self::TYPE_ID;
56
    }
57
58
    /**
59
     * Set the model for the field body.
60
     *
61
     * This method takes a string ID, or an array of IDs.
62
     *
63
     * @param mixed $model
64
     *
65
     * @throws Swift_RfcComplianceException
66
     */
67 123
    public function setFieldBodyModel($model)
68
    {
69 123
        $this->setId($model);
70 123
    }
71
72
    /**
73
     * Get the model for the field body.
74
     *
75
     * This method returns an array of IDs
76
     *
77
     * @return string[]
78
     */
79 92
    public function getFieldBodyModel()
80
    {
81 92
        return $this->getIds();
82
    }
83
84
    /**
85
     * Set the ID used in the value of this header.
86
     *
87
     * @param string|array $id
88
     *
89
     * @throws Swift_RfcComplianceException
90
     */
91 134
    public function setId($id)
92
    {
93 134
        if (is_array($id)) {
94
            $this->setIds($id);
95
        } else {
96 134
            $this->setIds(array($id));
97
        }
98 133
    }
99
100
    /**
101
     * Get the ID used in the value of this Header.
102
     *
103
     * If multiple IDs are set only the first is returned.
104
     *
105
     * @return string|null
106
     */
107 6
    public function getId()
108
    {
109 6
        if (count($this->_ids) > 0) {
110 6
            return $this->_ids[0];
111
        } else {
112
            return null;
113
        }
114
    }
115
116
    /**
117
     * Set a collection of IDs to use in the value of this Header.
118
     *
119
     * @param string[] $ids
120
     *
121
     * @throws Swift_RfcComplianceException
122
     */
123 137
    public function setIds(array $ids)
124
    {
125 137
        $actualIds = array();
126
127 137
        foreach ($ids as $id) {
128 137
            $this->_assertValidId($id);
129 136
            $actualIds[] = $id;
130 136
        }
131
132 136
        $this->clearCachedValueIf($this->_ids != $actualIds);
133 136
        $this->_ids = $actualIds;
134 136
    }
135
136
    /**
137
     * Get the list of IDs used in this Header.
138
     *
139
     * @return string[]
140
     */
141 94
    public function getIds()
142
    {
143 94
        return $this->_ids;
144
    }
145
146
    /**
147
     * Get the string value of the body in this Header.
148
     *
149
     * This is not necessarily RFC 2822 compliant since folding white space will
150
     * not be added at this stage (see {@see toString()} for that).
151
     *
152
     * @see toString()
153
     *
154
     * @throws Swift_RfcComplianceException
155
     *
156
     * @return string
157
     */
158 116
    public function getFieldBody()
159
    {
160 116
        if (!$this->getCachedValue()) {
161 116
            $angleAddrs = array();
162
163 116
            foreach ($this->_ids as $id) {
164 116
                $angleAddrs[] = '<' . $id . '>';
165 116
            }
166
167 116
            $this->setCachedValue(implode(' ', $angleAddrs));
168 116
        }
169
170 116
        return $this->getCachedValue();
171
    }
172
173
    /**
174
     * Throws an Exception if the id passed does not comply with RFC 2822.
175
     *
176
     * @param string $id
177
     *
178
     * @throws Swift_RfcComplianceException
179
     */
180 137
    private function _assertValidId($id)
181
    {
182 137
        if ($this->_emailValidator->isValidSimpleWrapper($id) === false) {
183 1
            throw new Swift_RfcComplianceException(
184 1
                'Invalid ID given <' . $id . '>'
185 1
            );
186
        }
187 136
    }
188
}
189