ContactService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 58
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A send() 0 5 1
A createMessage() 0 8 1
A composeBody() 0 4 1
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