SwiftmailerChannel::supports()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace Yokai\MessengerBundle\Channel;
4
5
use Swift_Mailer;
6
use Swift_Message;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Yokai\MessengerBundle\Channel\Swiftmailer\Configurator\SwiftMessageConfiguratorInterface;
9
use Yokai\MessengerBundle\Delivery;
10
use Yokai\MessengerBundle\Recipient\EmailRecipientInterface;
11
12
/**
13
 * @author Yann Eugoné <[email protected]>
14
 */
15
class SwiftmailerChannel implements ChannelInterface
16
{
17
    /**
18
     * @var Swift_Mailer
19
     */
20
    private $mailer;
21
22
    /**
23
     * @var SwiftMessageConfiguratorInterface
24
     */
25
    private $configurator;
26
27
    /**
28
     * @var array
29
     */
30
    private $defaults;
31
32
    /**
33
     * @param Swift_Mailer                      $mailer
34
     * @param SwiftMessageConfiguratorInterface $configurator
35
     * @param array                             $defaults
36
     */
37 9
    public function __construct(
38
        Swift_Mailer $mailer,
39
        SwiftMessageConfiguratorInterface $configurator,
40
        array $defaults
41
    ) {
42 9
        $this->mailer = $mailer;
43 9
        $this->configurator = $configurator;
44 9
        $this->defaults = $defaults;
45 9
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 6
    public function supports($recipient)
51
    {
52 6
        if (is_object($recipient)) {
53 4
            return $recipient instanceof EmailRecipientInterface;
54
        }
55
56 2
        if (is_string($recipient)) {
57 2
            return false !== filter_var($recipient, FILTER_VALIDATE_EMAIL);
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function configure(OptionsResolver $resolver)
67
    {
68
        $resolver
69 1
            ->setRequired(['from'])
70
            //todo
71
        ;
72 1
        foreach ($resolver->getDefinedOptions() as $option) {
73 1
            if (isset($this->defaults[$option])) {
74 1
                $resolver->setDefault($option, $this->defaults[$option]);
75
            }
76
        }
77 1
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 2
    public function handle(Delivery $delivery)
83
    {
84 2
        $mail = new Swift_Message();
85
86 2
        $this->configurator->configure($mail, $delivery);
87
88 2
        $this->mailer->send($mail);
89 2
    }
90
}
91