GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b5dfb6...a568a9 )
by Mario
40:05
created

Mailer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 53
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Mailer;
6
7
use Netgen\InformationCollection\API\Exception\EmailNotSentException;
8
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent;
9
use Netgen\InformationCollection\API\Mailer\MailerInterface;
10
11
class Mailer implements MailerInterface
12
{
13
    /**
14
     * @var \Swift_Mailer
15
     */
16
    protected $internalMailer;
17
18
    /**
19
     * Mailer constructor.
20
     *
21
     * @param \Swift_Mailer $internalMailer
22
     */
23
    public function __construct(\Swift_Mailer $internalMailer)
24
    {
25
        $this->internalMailer = $internalMailer;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function createAndSendMessage(EmailContent $data): void
32
    {
33
        $message = new \Swift_Message();
34
35
        try {
36
            $message->setTo($data->getRecipient());
37
        } catch (\Swift_RfcComplianceException $e) {
38
            throw new EmailNotSentException('recipient', $e->getMessage());
39
        }
40
41
        try {
42
            $message->setFrom($data->getSender());
43
        } catch (\Swift_RfcComplianceException $e) {
44
            throw new EmailNotSentException('sender', $e->getMessage());
45
        }
46
47
        $message->setSubject($data->getSubject());
48
        $message->setBody($data->getBody(), 'text/html');
49
50
        if ($data->hasAttachments()) {
51
            foreach ($data->getAttachments() as $attachment) {
0 ignored issues
show
Bug introduced by
The expression $data->getAttachments() of type array<integer,object<eZ\...BinaryFile\Value>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
                $message->attach(
53
                    \Swift_Attachment::fromPath($attachment->inputUri, $attachment->mimeType)
54
                        ->setFilename($attachment->fileName)
55
                );
56
            }
57
        }
58
59
        if (!$this->internalMailer->send($message)) {
60
            throw new EmailNotSentException('send', 'invalid mailer configuration?');
61
        }
62
    }
63
}
64