Cancelled
Push — develop ( 5a5c91...8665a3 )
by Florian
33s queued 10s
created

EmailTemplateRepository::getAllAddresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\DTO\EmailTemplate;
6
use App\DTO\Provider;
7
use Symfony\Component\Serializer\SerializerInterface;
8
use function array_filter;
9
use function array_map;
10
use function array_unique;
11
use function in_array;
12
use function stripos;
13
14
class EmailTemplateRepository
15
{
16
    /** @var EmailTemplate[] */
17
    private array $emailTemplates;
18
    private ProviderRepository $providerRepository;
19
20
    /**
21
     * @param array               $emailTemplates
22
     * @param SerializerInterface $serializer
23
     * @param ProviderRepository  $providerRepository
24
     */
25
    public function __construct(array $emailTemplates, SerializerInterface $serializer, ProviderRepository $providerRepository)
26
    {
27
        $this->emailTemplates = $serializer->denormalize($emailTemplates, EmailTemplate::class . '[]');
28
        $this->providerRepository = $providerRepository;
29
    }
30
31
    /**
32
     * @param string $from
33
     * @param string $subject
34
     *
35
     * @return EmailTemplate|null
36
     */
37
    public function find(string $from, string $subject): ?EmailTemplate
38
    {
39
        foreach ($this->emailTemplates as $template) {
40
            if ($from === $template->getFrom()) {
41
                // First, try to match an email template containing a particular subject keyword
42
                if (null !== $template->getSubjectKeyword()) {
43
                    if (false !== stripos($subject, $template->getSubjectKeyword())) {
44
                        return $template;
45
                    }
46
47
                    continue;
48
                }
49
50
                return $template;
51
            }
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @return string[]
59
     */
60
    public function getAllAddresses(): array
61
    {
62
        $addresses = array_map(fn(EmailTemplate $template) => $template->getAddress(), $this->emailTemplates);
63
64
        return array_unique($addresses);
65
    }
66
67
    /**
68
     * @param string $mainProviderId
69
     *
70
     * @return string[]
71
     */
72
    public function getAddressesByMainProvider(string $mainProviderId): array
73
    {
74
        $providers = $this->providerRepository->getProvidersByMainProvider($mainProviderId);
75
        $providerIds = array_map(fn(Provider $provider) => $provider->getId(), $providers);
76
77
        $templates = array_filter($this->emailTemplates, fn(EmailTemplate $template) =>
78
            in_array($template->getProviderId(), $providerIds, true)
79
        );
80
81
        $addresses = array_map(fn(EmailTemplate $template) => $template->getAddress(), $templates);
82
83
        return array_unique($addresses);
84
    }
85
}
86