Completed
Push — develop ( b559a4...dfd31d )
by
unknown
10:04
created

Message::updateAddressList()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 23
nc 4
nop 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Message.php */
11
namespace Core\Mail;
12
13
use Auth\Entity\UserInterface;
14
use Traversable;
15
use Zend\Mail\Address;
16
use Zend\Mail\AddressList;
17
use Zend\Mail\Exception;
18
use Zend\Mail\Message as ZfMessage;
19
20
class Message extends ZfMessage
21
{
22
    
23
    public function __construct(array $options = array())
24
    {
25
        $this->setOptions($options);
26
    }
27
    
28
    public function setOptions($options)
29
    {
30 View Code Duplication
        if (!is_array($options) && !$options instanceof \Traversable) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            throw new \InvalidArgumentException(
32
                sprintf(
33
                    'Expected $options to be an array or \Traversable, but received %s',
34
                    (is_object($options) ? 'instance of ' . get_class($options) : 'skalar')
35
                )
36
            );
37
        }
38
        
39 View Code Duplication
        foreach ($options as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
            $method = "set$key";
41
            if (method_exists($this, $method)) {
42
                $this->$method($value);
43
            }
44
        }
45
        return $this;
46
    }
47
48
    protected function updateAddressList(AddressList $addressList, $emailOrAddressOrList, $name, $callingMethod)
49
    {
50
        if (null === $emailOrAddressOrList) {
51
            return;
52
        }
53
54
        if ($emailOrAddressOrList instanceOf UserInterface) {
55
            parent::updateAddressList(
56
                         $addressList,
57
                         $emailOrAddressOrList->getInfo()->getEmail(),
58
                         $emailOrAddressOrList->getInfo()->getDisplayName(false),
59
                         $callingMethod
60
            );
61
            return;
62
        }
63
64
        if (is_array($emailOrAddressOrList)) {
65
            $list = new AddressList();
66
            foreach ($emailOrAddressOrList as $email => $displayName) {
67
                if ($displayName instanceOf UserInterface) {
68
                    $info = $displayName->getInfo();
69
                    $list->add($info->getEmail(), $info->getDisplayName(false));
70
                    continue;
71
                }
72
73
                if (is_int($email)) {
74
                    $email = $displayName;
75
                    $displayName = null;
76
                }
77
78
                $list->add($email, $displayName);
79
            }
80
            $emailOrAddressOrList = $list;
81
        }
82
83
        parent::updateAddressList($addressList, $emailOrAddressOrList, $name, $callingMethod);
84
    }
85
86
87
}
88