Completed
Pull Request — 5.x (#16)
by Lars
05:10
created

Swift_Mime_Headers_PathHeader::setAddress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 9
nc 3
nop 1
crap 3
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
 * A Path Header in Swift Mailer, such a Return-Path.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_Headers_PathHeader 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 address in this Header (if specified).
20
     *
21
     * @var string
22
     */
23
    private $_address;
24
25
    /**
26
     * The strict EmailValidator
27
     *
28
     * @var Swift_EmailValidatorBridge
29
     */
30
    private $_emailValidator;
31
32
    /**
33
     * Creates a new PathHeader with the given $name.
34
     *
35
     * @param string                     $name
36
     * @param Swift_EmailValidatorBridge $emailValidator
37
     */
38 41
    public function __construct($name, $emailValidator)
39
    {
40 41
        $this->setFieldName($name);
41 41
        $this->_emailValidator = $emailValidator;
42 41
        parent::__construct();
43 41
    }
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_PATH;
56
    }
57
58
    /**
59
     * Set the model for the field body.
60
     * This method takes a string for an address.
61
     *
62
     * @param string $model
63
     *
64
     * @throws Swift_RfcComplianceException
65
     */
66 32
    public function setFieldBodyModel($model)
67
    {
68 32
        $this->setAddress($model);
69 32
    }
70
71
    /**
72
     * Get the model for the field body.
73
     * This method returns a string email address.
74
     *
75
     * @return mixed
76
     */
77 2
    public function getFieldBodyModel()
78
    {
79 2
        return $this->getAddress();
80
    }
81
82
    /**
83
     * Set the Address which should appear in this Header.
84
     *
85
     * @param string $address
86
     *
87
     * @throws Swift_RfcComplianceException
88
     */
89 38
    public function setAddress($address)
90
    {
91 38
        if (null === $address) {
92
            $this->_address = null;
93 38
        } elseif ('' == $address) {
94 3
            $this->_address = '';
95
        } else {
96 35
            $this->_assertValidAddress($address);
97 34
            $this->_address = $address;
98
        }
99
100 37
        $this->setCachedValue(null);
101 37
    }
102
103
    /**
104
     * Get the address which is used in this Header (if any).
105
     *
106
     * Null is returned if no address is set.
107
     *
108
     * @return string
109
     */
110 4
    public function getAddress()
111
    {
112 4
        return $this->_address;
113
    }
114
115
    /**
116
     * Get the string value of the body in this Header.
117
     *
118
     * This is not necessarily RFC 2822 compliant since folding white space will
119
     * not be added at this stage (see {@link toString()} for that).
120
     *
121
     * @see toString()
122
     *
123
     * @return string
124
     */
125 33
    public function getFieldBody()
126
    {
127 33
        if (!$this->getCachedValue()) {
128 33
            if (isset($this->_address)) {
129 33
                $this->setCachedValue('<' . $this->_address . '>');
130
            }
131
        }
132
133 33
        return $this->getCachedValue();
134
    }
135
136
    /**
137
     * Throws an Exception if the address passed does not comply with RFC 2822.
138
     *
139
     * @param string $address
140
     *
141
     * @throws Swift_RfcComplianceException If address is invalid
142
     */
143 35
    private function _assertValidAddress($address)
144
    {
145 35
        if ($this->_emailValidator->isValidWrapper($address) === false) {
146 1
            throw new Swift_RfcComplianceException('Address set in PathHeader does not comply with addr-spec of RFC 2822.');
147
        }
148 34
    }
149
}
150