Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| WebmailConnectionManager | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| connectDriver | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Mail\Application\Service\Connection; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Security\Encryption\EncryptionException; |
| 12 | use App\Core\Security\Encryption\EncryptionServiceInterface; |
| 13 | use App\Modules\Mail\Domain\Contract\MailProtocolDriverInterface; |
| 14 | use App\Modules\Mail\Domain\Contract\MailProtocolDriverRegistryInterface; |
| 15 | use App\Modules\Mail\Domain\Exception\WebmailAuthException; |
| 16 | use App\Modules\Mail\Domain\Model\ClientMailbox; |
| 17 | use App\Modules\Mail\Domain\Repository\MailRepositoryInterface; |
| 18 | |
| 19 | /** |
| 20 | * Concrete connection manager for resolving, decrypting and authenticating mail protocol drivers. |
| 21 | */ |
| 22 | final readonly class WebmailConnectionManager implements WebmailConnectionManagerInterface |
| 23 | { |
| 24 | /** |
| 25 | * WebmailConnectionManager constructor. |
| 26 | * |
| 27 | * @param MailRepositoryInterface $mailRepo Mail repository for server lookup. |
| 28 | * @param EncryptionServiceInterface $encryption Encryption service for credential decryption. |
| 29 | * @param MailProtocolDriverRegistryInterface $driverRegistry Driver registry by protocol type. |
| 30 | */ |
| 31 | public function __construct( |
| 32 | private MailRepositoryInterface $mailRepo, |
| 33 | private EncryptionServiceInterface $encryption, |
| 34 | private MailProtocolDriverRegistryInterface $driverRegistry |
| 35 | ) { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | public function connectDriver(ClientMailbox $mailbox): MailProtocolDriverInterface |
| 42 | { |
| 43 | $server = $this->mailRepo->findMailServerById($mailbox->mailServerId); |
| 44 | $driver = $this->driverRegistry->getDriver($mailbox->protocolType); |
| 45 | |
| 46 | try { |
| 47 | $plainPass = $this->encryption->decrypt($mailbox->password); |
| 48 | } catch (EncryptionException $e) { |
| 49 | throw new WebmailAuthException( |
| 50 | 'Mailbox credentials could not be decrypted. Please re-enter password in settings.', |
| 51 | previous: $e |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | $activeMailbox = $mailbox->withDecryptedPassword($plainPass); |
| 56 | $driver->connect($activeMailbox, $server); |
| 57 | |
| 58 | return $driver; |
| 59 | } |
| 60 | } |