Completed
Push — master ( e347f9...ce4937 )
by Alexey
72:04 queued 31:57
created

Mailer::send()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 9
nop 3
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
     * EmailSender constructor.
39
     *
40
     * @param ContainerInterface $container
41
     * @param LoggerInterface $logger
0 ignored issues
show
Bug introduced by
There is no parameter named $logger. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     */
43
    public function __construct(ContainerInterface $container)
44
    {
45
        $this->container = $container;
46
    }
47
48
    /**
49
     * Get senders
50
     *
51
     * @return array
52
     */
53
    public function getSenders(): array
54
    {
55
        return $this->senders;
56
    }
57
58
    /**
59
     * Set senders
60
     *
61
     * @param array $senders
62
     */
63
    public function setSenders(array $senders): void
64
    {
65
        $this->senders = $senders;
66
    }
67
68
    /**
69
     * Send email template
70
     *
71
     * @param TemplateInterface $template
72
     * @param array|string $emails
73
     * @param null|MessageOptionsInterface $options
74
     *
75
     * @return int
76
     */
77
    public function send(TemplateInterface $template, $emails, ?MessageOptionsInterface $options = null): int
78
    {
79
        $sentCount = 0;
80
81
        foreach ($this->senders as $config) {
82
//            try {
83
            $concreteTemplate = clone $template;
84
            $sender = array_merge($config['sender'], ['options' => $options]);
85
            $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...
Bug introduced by
It seems like $sender['options'] ?? array() can also be of type object<SfCod\EmailEngine...essageOptionsInterface>; however, SfCod\EmailEngineBundle\...er\Mailer::makeSender() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
87
            if ($concreteTemplate instanceof RepositoryAwareInterface) {
88
                $concreteTemplate->setRepository($this->makeRepository($config['repository']['class'], $concreteTemplate, $config['repository']['arguments']));
89
            }
90
91
            if ($concreteTemplate instanceof ParametersAwareInterface) {
92
                $concreteTemplate->setParameterResolver($this->container->get(ParameterResolverInterface::class));
93
            }
94
95
            if ($concreteSender->send($concreteTemplate, $emails)) {
96
                ++$sentCount;
97
98
                break;
99
            }
100
//            } catch (RepositoryUnavailableException $e) {
101
//                if ($this->container->get('kernel')->isDebug()) {
102
//                    $this->container->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
103
//                }
104
//
105
//                // Try next sender
106
//            }
107
        }
108
109
        return $sentCount;
110
    }
111
112
    /**
113
     * Make email engine sender
114
     *
115
     * @param string $sender
116
     * @param array $options
117
     *
118
     * @return SenderInterface
119
     */
120
    protected function makeSender(string $sender, array $options = []): SenderInterface
121
    {
122
        /** @var SenderInterface $sender */
123
        $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...
124
125
        if (false === empty($options)) {
126
            $sender->setOptions($options);
0 ignored issues
show
Documentation introduced by
$options is of type array, but the function expects a object<SfCod\EmailEngine...essageOptionsInterface>.

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...
127
        }
128
129
        return $sender;
130
    }
131
132
    /**
133
     * Make email engine repository
134
     *
135
     * @param string $repository
136
     * @param TemplateInterface $template
137
     * @param array $arguments
138
     *
139
     * @return RepositoryInterface
140
     */
141
    protected function makeRepository(string $repository, TemplateInterface $template, array $arguments = []): RepositoryInterface
142
    {
143
        /** @var RepositoryInterface $repository */
144
        $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...
145
        $repository->connect($template, $arguments);
146
147
        return $repository;
148
    }
149
}
150