ContactService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Service;
5
6
use Acelaya\Website\Options\MailOptions;
7
use Swift_Mailer;
8
use Zend\Expressive\Template\TemplateRendererInterface;
9
10
class ContactService implements ContactServiceInterface
11
{
12
    const TEMPLATE = 'Acelaya::emails/contact';
13
14
    /**
15
     * @var Swift_Mailer
16
     */
17
    protected $mailer;
18
    /**
19
     * @var TemplateRendererInterface
20
     */
21
    protected $renderer;
22
    /**
23
     * @var MailOptions
24
     */
25
    protected $options;
26
27
    public function __construct(Swift_Mailer $mailer, TemplateRendererInterface $renderer, MailOptions $options)
28
    {
29
        $this->mailer = $mailer;
30
        $this->renderer = $renderer;
31
        $this->options = $options;
32
    }
33
34 1
    /**
35
     * Sends the email and returns the result
36 1
     *
37 1
     * @param array $messageData
38 1
     * @return bool
39 1
     */
40
    public function send(array $messageData): bool
41
    {
42
        $result = $this->mailer->send($this->createMessage($messageData));
0 ignored issues
show
Documentation introduced by
$this->createMessage($messageData) is of type object<Swift_Mime_MimePart>, but the function expects a object<Swift_Mime_Message>.

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...
43
        return $result === 1;
44
    }
45
46
    /**
47 1
     * @param array $messageData
48
     * @return \Swift_Mime_MimePart
49 1
     */
50 1
    private function createMessage(array $messageData): \Swift_Mime_MimePart
51
    {
52
        return \Swift_Message::newInstance($this->options->getSubject())
53
                             ->setTo($this->options->getTo())
54
                             ->setFrom($this->options->getFrom())
55
                             ->setReplyTo($messageData['email'])
56
                             ->setBody($this->composeBody($messageData), 'text/html');
57 1
    }
58
59 1
    /**
60 1
     * @param array $messageData
61 1
     * @return string
62 1
     */
63 1
    private function composeBody(array $messageData): string
64
    {
65
        return $this->renderer->render(self::TEMPLATE, $messageData);
66
    }
67
}
68