Completed
Push — master ( e6ae53...3ea501 )
by Sam
10:01 queued 45s
created

SwiftPlugin::setFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control\Email;
4
5
class SwiftPlugin implements \Swift_Events_SendListener
6
{
7
    /**
8
     * Before sending a message make sure all our overrides are taken into account
9
     *
10
     * @param \Swift_Events_SendEvent $evt
11
     */
12
    public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
13
    {
14
15
        /** @var \Swift_Message $message */
16
        $message = $evt->getMessage();
17
        $sendAllTo = Email::config()->send_all_emails_to;
0 ignored issues
show
Documentation introduced by
The property send_all_emails_to does not exist on object<SilverStripe\Core\Config\Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
18
        $ccAllTo = Email::config()->cc_all_emails_to;
0 ignored issues
show
Documentation introduced by
The property cc_all_emails_to does not exist on object<SilverStripe\Core\Config\Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
19
        $bccAllTo = Email::config()->bcc_all_emails_to;
0 ignored issues
show
Documentation introduced by
The property bcc_all_emails_to does not exist on object<SilverStripe\Core\Config\Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
20
        $sendAllFrom = Email::config()->send_all_emails_from;
0 ignored issues
show
Documentation introduced by
The property send_all_emails_from does not exist on object<SilverStripe\Core\Config\Config_ForClass>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
21
22
        if (!empty($sendAllTo)) {
23
            $this->setTo($message, $sendAllTo);
24
        }
25
26
        if (!empty($ccAllTo)) {
27
            if (!is_array($ccAllTo)) {
28
                $ccAllTo = array($ccAllTo => null);
29
            }
30
            foreach ($ccAllTo as $address => $name) {
31
                $message->addCc($address, $name);
32
            }
33
        }
34
35
        if (!empty($bccAllTo)) {
36
            if (!is_array($bccAllTo)) {
37
                $bccAllTo = array($bccAllTo => null);
38
            }
39
            foreach ($bccAllTo as $address => $name) {
40
                $message->addBcc($address, $name);
41
            }
42
        }
43
44
        if (!empty($sendAllFrom)) {
45
            $this->setFrom($message, $sendAllFrom);
46
        }
47
    }
48
49
    /**
50
     * @param \Swift_Mime_Message $message
51
     * @param string $to
52
     */
53
    protected function setTo($message, $to)
54
    {
55
        $headers = $message->getHeaders();
56
        $origTo = $message->getTo();
57
        $cc = $message->getCc();
58
        $bcc = $message->getBcc();
59
60
        // set default recipient and remove all other recipients
61
        $message->setTo($to);
62
        $headers->removeAll('Cc');
63
        $headers->removeAll('Bcc');
64
65
        // store the old data as X-Original-* Headers for debugging
66
        $headers->addMailboxHeader('X-Original-To', $origTo);
67
        $headers->addMailboxHeader('X-Original-Cc', $cc);
68
        $headers->addMailboxHeader('X-Original-Bcc', $bcc);
69
    }
70
71
    /**
72
     * @param \Swift_Mime_Message $message
73
     * @param string $from
74
     */
75
    protected function setFrom($message, $from)
76
    {
77
        $headers = $message->getHeaders();
78
        $origFrom = $message->getFrom();
79
        $headers->addMailboxHeader('X-Original-From', $origFrom);
80
        $message->setFrom($from);
81
    }
82
83
    public function sendPerformed(\Swift_Events_SendEvent $evt)
84
    {
85
        // noop
86
    }
87
}
88