Test Failed
Pull Request — master (#49)
by Alexander
04:20 queued 01:52
created

EmailFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 49
c 3
b 1
f 0
dl 0
loc 100
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertAddressesToStrings() 0 9 3
A convertStringsToAddresses() 0 19 4
C create() 0 52 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mailer\Symfony;
6
7
use DateTimeImmutable;
8
use Symfony\Component\Mime\Address;
9
use Symfony\Component\Mime\Email;
10
use Yiisoft\Mailer\MessageInterface;
11
12
/**
13
 * @internal
14
 */
15
final class EmailFactory
16
{
17
    public function create(MessageInterface $message): Email
18
    {
19
        $email = (new Email())
20
            ->from(...$this->convertStringsToAddresses($message->getFrom()))
21
            ->to(...$this->convertStringsToAddresses($message->getTo()))
22
            ->replyTo(...$this->convertStringsToAddresses($message->getReplyTo()))
23
            ->cc(...$this->convertStringsToAddresses($message->getCc()))
24
            ->bcc(...$this->convertStringsToAddresses($message->getBcc()))
25
            ->subject($message->getSubject())
26
            ->priority($message->getPriority())
27
            ->text($message->getTextBody(), $message->getCharset())
28
            ->html($message->getHtmlBody(), $message->getCharset());
29
30
        $returnPath = $message->getReturnPath();
31
        if ($returnPath !== '') {
32
            $email->returnPath($returnPath);
33
        }
34
35
        $sender = $message->getSender();
36
        if ($sender !== '') {
37
            $email->sender($sender);
38
        }
39
40
        $date = $message->getDate();
41
        if ($date !== null) {
42
            $email->date($date);
43
        }
44
45
        $emailHeaders = $email->getHeaders();
46
        foreach ($message->getHeaders() as $name => $values) {
0 ignored issues
show
Bug introduced by
The method getHeaders() does not exist on Yiisoft\Mailer\MessageInterface. Did you maybe mean getHeader()? ( Ignorable by Annotation )

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

46
        foreach ($message->/** @scrutinizer ignore-call */ getHeaders() as $name => $values) {

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...
47
            foreach ($values as $value) {
48
                match ($name) {
49
                    'Date' => $emailHeaders->addDateHeader($name, new DateTimeImmutable($value)),
50
                    'Message-ID' => $emailHeaders->addIdHeader($name, $value),
51
                    default => $emailHeaders->addTextHeader($name, $value),
52
                };
53
            }
54
        }
55
56
        foreach ($message->getAttachments() as $file) {
0 ignored issues
show
Bug introduced by
The method getAttachments() does not exist on Yiisoft\Mailer\MessageInterface. ( Ignorable by Annotation )

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

56
        foreach ($message->/** @scrutinizer ignore-call */ getAttachments() as $file) {

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...
57
            $file->path() === null
58
                ? $email->attach((string) $file->content(), $file->name(), $file->contentType())
59
                : $email->attachFromPath($file->path(), $file->name(), $file->contentType());
60
        }
61
62
        foreach ($message->getEmbeddedFiles() as $file) {
0 ignored issues
show
Bug introduced by
The method getEmbeddedFiles() does not exist on Yiisoft\Mailer\MessageInterface. ( Ignorable by Annotation )

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

62
        foreach ($message->/** @scrutinizer ignore-call */ getEmbeddedFiles() as $file) {

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...
63
            $file->path() === null
64
                ? $email->embed((string) $file->content(), $file->name(), $file->contentType())
65
                : $email->embedFromPath($file->path(), $file->name(), $file->contentType());
66
        }
67
68
        return $email;
69
    }
70
71
    /**
72
     * Converts address instances to their string representations.
73
     *
74
     * @param Address[] $addresses
75
     *
76
     * @return array<string, string>|string
77
     */
78
    private function convertAddressesToStrings(array $addresses): array|string
0 ignored issues
show
Unused Code introduced by
The method convertAddressesToStrings() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
79
    {
80
        $strings = [];
81
82
        foreach ($addresses as $address) {
83
            $strings[$address->getAddress()] = $address->getName();
84
        }
85
86
        return empty($strings) ? '' : $strings;
87
    }
88
89
    /**
90
     * Converts string representations of address to their instances.
91
     *
92
     * @param array<int|string, string>|string $strings
93
     *
94
     * @return Address[]
95
     */
96
    private function convertStringsToAddresses(array|string $strings): array
97
    {
98
        if (is_string($strings)) {
0 ignored issues
show
introduced by
The condition is_string($strings) is always false.
Loading history...
99
            return [new Address($strings)];
100
        }
101
102
        $addresses = [];
103
104
        foreach ($strings as $address => $name) {
105
            if (!is_string($address)) {
106
                // email address without name
107
                $addresses[] = new Address($name);
108
                continue;
109
            }
110
111
            $addresses[] = new Address($address, $name);
112
        }
113
114
        return $addresses;
115
    }
116
}
117