Completed
Push — master ( c8e7e4...d14267 )
by Alexey
38:24
created

Mailer::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SfCod\EmailEngineBundle\Mailer;
4
5
use Psr\Log\LoggerInterface;
6
use SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException;
7
use SfCod\EmailEngineBundle\Repository\RepositoryInterface;
8
use SfCod\EmailEngineBundle\Sender\MessageOptionsInterface;
9
use SfCod\EmailEngineBundle\Sender\SenderInterface;
10
use SfCod\EmailEngineBundle\Template\ParametersAwareInterface;
11
use SfCod\EmailEngineBundle\Template\Params\ParameterResolverInterface;
12
use SfCod\EmailEngineBundle\Template\RepositoryAwareInterface;
13
use SfCod\EmailEngineBundle\Template\TemplateInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * Class Mailer
18
 *
19
 * @author Virchenko Maksim <[email protected]>
20
 *
21
 * @package SfCod\EmailEngineBundle\Sender
22
 */
23
class Mailer
24
{
25
    /**
26
     * Senders for email sending
27
     *
28
     * @var SenderInterface[]
29
     */
30
    private $senders = [];
31
32
    /**
33
     * @var ContainerInterface
34
     */
35
    private $container;
36
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42
    /**
43
     * EmailSender constructor.
44
     *
45
     * @param ContainerInterface $container
46
     */
47
    public function __construct(ContainerInterface $container)
48
    {
49
        $this->container = $container;
50
    }
51
52
    /**
53
     * Get senders
54
     *
55
     * @return array
56
     */
57
    public function getSenders(): array
58
    {
59
        return $this->senders;
60
    }
61
62
    /**
63
     * Set senders
64
     *
65
     * @param array $senders
66
     */
67
    public function setSenders(array $senders): void
68
    {
69
        $this->senders = $senders;
70
    }
71
72
    /**
73
     * Set Logger
74
     *
75
     * @param LoggerInterface $logger
76
     */
77
    public function setLogger(LoggerInterface $logger): void
78
    {
79
        $this->logger = $logger;
80
    }
81
82
    /**
83
     * Send email template
84
     *
85
     * @param TemplateInterface $template
86
     * @param array|string $emails
87
     * @param null|MessageOptionsInterface $options
88
     *
89
     * @return int
90
     */
91
    public function send(TemplateInterface $template, $emails, ?MessageOptionsInterface $options = null): int
92
    {
93
        $sentCount = 0;
94
95
        foreach ($this->senders as $config) {
96
            try {
97
                $concreteTemplate = clone $template;
98
                $sender = array_merge($config['sender'], ['options' => $options]);
99
                $concreteSender = $this->makeSender($sender['class'], $sender['options']);
0 ignored issues
show
Documentation introduced by
$sender['class'] is of type null|object<SfCod\EmailE...essageOptionsInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
101
                if ($concreteTemplate instanceof RepositoryAwareInterface) {
102
                    $concreteTemplate->setRepository($this->makeRepository($config['repository']['class'], $concreteTemplate, $config['repository']['arguments']));
103
                }
104
105
                if ($concreteTemplate instanceof ParametersAwareInterface) {
106
                    $concreteTemplate->setParameterResolver($this->container->get(ParameterResolverInterface::class));
107
                }
108
109
                if ($concreteSender->send($concreteTemplate, $emails)) {
110
                    ++$sentCount;
111
112
                    break;
113
                }
114
            } catch (RepositoryUnavailableException $e) {
115
                $this->logger->error($e->getMessage(), ['exception' => $e]);
116
117
                // Try next sender
118
            }
119
        }
120
121
        return $sentCount;
122
    }
123
124
    /**
125
     * Make email engine sender
126
     *
127
     * @param string $sender
128
     * @param MessageOptionsInterface|null $options
129
     *
130
     * @return SenderInterface
131
     */
132
    protected function makeSender(string $sender, ?MessageOptionsInterface $options = null): SenderInterface
133
    {
134
        /** @var SenderInterface $sender */
135
        $sender = $this->container->get($sender);
0 ignored issues
show
Documentation introduced by
$sender is of type object<SfCod\EmailEngine...Sender\SenderInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
137
        if ($options) {
138
            $sender->setOptions($options);
139
        }
140
141
        return $sender;
142
    }
143
144
    /**
145
     * Make email engine repository
146
     *
147
     * @param string $repository
148
     * @param TemplateInterface $template
149
     * @param array $arguments
150
     *
151
     * @return RepositoryInterface
152
     */
153
    protected function makeRepository(string $repository, TemplateInterface $template, array $arguments = []): RepositoryInterface
154
    {
155
        /** @var RepositoryInterface $repository */
156
        $repository = $this->container->get($repository);
0 ignored issues
show
Documentation introduced by
$repository is of type object<SfCod\EmailEngine...ry\RepositoryInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
        $repository->connect($template, $arguments);
158
159
        return $repository;
160
    }
161
}
162