Completed
Push — 5.x ( f07c3e...50a512 )
by Lars
06:07
created

Swift_Mime_Headers_IdentificationHeader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.62%
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 168
ccs 41
cts 42
cp 0.9762
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFieldType() 0 4 1
A setFieldBodyModel() 0 4 1
A getFieldBodyModel() 0 4 1
A setId() 0 8 2
A getId() 0 8 2
A setIds() 0 12 2
A getIds() 0 4 1
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
     * Creates a new IdentificationHeader with the given $name and $id.
29
     *
30
     * @param string                     $name
31
     * @param Swift_EmailValidatorBridge $emailValidator
32
     */
33 140
    public function __construct($name, Swift_EmailValidatorBridge $emailValidator)
34
    {
35 140
        $this->setFieldName($name);
36 140
        $this->_emailValidator = $emailValidator;
0 ignored issues
show
Bug introduced by
The property _emailValidator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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