Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MailProtocolDriverRegistry
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
9.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 getDriver
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 supports
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 registerDriver
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Mail\Infrastructure\Protocol;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Mail\Domain\Contract\MailProtocolDriverInterface;
12use App\Modules\Mail\Domain\Contract\MailProtocolDriverRegistryInterface;
13use App\Modules\Mail\Infrastructure\Protocol\Exchange\ExchangeProtocolDriver;
14use App\Modules\Mail\Infrastructure\Protocol\Imap\ImapProtocolDriver;
15use App\Modules\Mail\Infrastructure\Protocol\Jmap\JmapProtocolDriver;
16use InvalidArgumentException;
17
18/**
19 * Mail Protocol Driver Registry.
20 *
21 * Dispatches protocol driver instances based on mailbox configuration type.
22 *
23 * @package App\Modules\Mail\Infrastructure\Protocol
24 */
25final class MailProtocolDriverRegistry implements MailProtocolDriverRegistryInterface
26{
27    /** @var array<string, MailProtocolDriverInterface> */
28    private array $drivers = [];
29
30    /**
31     * MailProtocolDriverRegistry constructor.
32     *
33     * @param array<string, MailProtocolDriverInterface> $initialDrivers Optional driver map.
34     */
35    public function __construct(array $initialDrivers = [])
36    {
37        foreach ($initialDrivers as $protocol => $driver) {
38            $this->registerDriver($protocol, $driver);
39        }
40
41        if (!isset($this->drivers['imap'])) {
42            $this->registerDriver('imap', new ImapProtocolDriver());
43        }
44        if (!isset($this->drivers['jmap'])) {
45            $this->registerDriver('jmap', new JmapProtocolDriver());
46        }
47        if (!isset($this->drivers['exchange'])) {
48            $this->registerDriver('exchange', new ExchangeProtocolDriver());
49        }
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    public function getDriver(string $protocolType): MailProtocolDriverInterface
56    {
57        $key = strtolower(trim($protocolType));
58        if (!isset($this->drivers[$key])) {
59            throw new InvalidArgumentException(sprintf('Unsupported mail protocol driver: "%s".', $protocolType));
60        }
61
62        return clone $this->drivers[$key];
63    }
64
65    /**
66     * {@inheritdoc}
67     */
68    public function supports(string $protocolType): bool
69    {
70        $key = strtolower(trim($protocolType));
71        return isset($this->drivers[$key]);
72    }
73
74    /**
75     * {@inheritdoc}
76     */
77    public function registerDriver(string $protocolType, MailProtocolDriverInterface $driver): void
78    {
79        $this->drivers[strtolower(trim($protocolType))] = $driver;
80    }
81}