MessageEventListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\EventListener\Mailer;
15
16
use Symfony\Component\Mailer\Event\MessageEvent;
17
use Symfony\Component\Mime\Address;
18
use Symfony\Component\Mime\Email;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class MessageEventListener
24
{
25
    private string $fromEmailAddress;
26
27
    public function __construct(string $fromEmailAddress)
28
    {
29
        $this->fromEmailAddress = $fromEmailAddress;
30
    }
31
32
    public function __invoke(MessageEvent $messageEvent): void
33
    {
34
        $message = $messageEvent->getMessage();
35
        if (!$message instanceof Email) {
36
            return;
37
        }
38
        // symfony/mime 5.2 deprecated fromString
39
        if (method_exists(Address::class, 'create')) {
40
            $toEmailAddress = Address::create($this->fromEmailAddress);
41
        } else {
42
            $toEmailAddress = Address::fromString($this->fromEmailAddress);
0 ignored issues
show
Bug introduced by
The method fromString() does not exist on Symfony\Component\Mime\Address. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            /** @scrutinizer ignore-call */ 
43
            $toEmailAddress = Address::fromString($this->fromEmailAddress);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        }
44
        $message->from($toEmailAddress);
45
    }
46
}
47