EmailTemplateProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 60
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddressesByMainProvider() 0 12 1
A getAllAddresses() 0 5 1
A __construct() 0 4 1
A find() 0 18 5
1
<?php
2
3
namespace App\DataProvider;
4
5
use App\DTO\EmailTemplate;
6
use App\DTO\Provider;
7
use Symfony\Component\Serializer\Exception\ExceptionInterface;
8
use Symfony\Component\Serializer\SerializerInterface;
9
use function array_filter;
10
use function array_map;
11
use function array_unique;
12
use function in_array;
13
use function str_contains;
14
15
class EmailTemplateProvider
16
{
17
    /** @var EmailTemplate[] */
18
    private array $emailTemplates;
19
    private ProviderProvider $providerProvider;
20
21
    /**
22
     * @throws ExceptionInterface
23
     */
24 5
    public function __construct(array $emailTemplates, SerializerInterface $serializer, ProviderProvider $providerProvider)
25
    {
26 5
        $this->emailTemplates = $serializer->denormalize($emailTemplates, EmailTemplate::class . '[]');
27 5
        $this->providerProvider = $providerProvider;
28 5
    }
29
30 3
    public function find(string $from, string $subject): ?EmailTemplate
31
    {
32 3
        foreach ($this->emailTemplates as $template) {
33 3
            if ($from === $template->getFrom()) {
34
                // First, try to match an email template containing a particular subject keyword
35 2
                if (null !== $template->getSubjectKeyword()) {
36 2
                    if (str_contains($subject, $template->getSubjectKeyword())) {
37 1
                        return $template;
38
                    }
39
40 1
                    continue;
41
                }
42
43 1
                return $template;
44
            }
45
        }
46
47 1
        return null;
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53 1
    public function getAllAddresses(): array
54
    {
55 1
        $addresses = array_map(static fn(EmailTemplate $template) => $template->getAddress(), $this->emailTemplates);
56
57 1
        return array_unique($addresses);
58
    }
59
60
    /**
61
     * @return string[]
62
     */
63 1
    public function getAddressesByMainProvider(string $mainProviderName): array
64
    {
65 1
        $providers = $this->providerProvider->getProvidersByMainProvider($mainProviderName);
66 1
        $providerNames = array_map(static fn(Provider $provider) => $provider->getName(), $providers);
67
68 1
        $templates = array_filter($this->emailTemplates, static fn(EmailTemplate $template) =>
69 1
            in_array($template->getProviderName(), $providerNames, true)
70 1
        );
71
72 1
        $addresses = array_map(static fn(EmailTemplate $template) => $template->getAddress(), $templates);
73
74 1
        return array_unique($addresses);
75
    }
76
}
77