MessageEventListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 13 3
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