ImapReceiverStrategy::resolveDsn()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 25
ccs 0
cts 19
cp 0
crap 30
rs 9.4555
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Derafu: Biblioteca PHP (Núcleo).
7
 * Copyright (C) Derafu <https://www.derafu.org>
8
 *
9
 * Este programa es software libre: usted puede redistribuirlo y/o modificarlo
10
 * bajo los términos de la Licencia Pública General Affero de GNU publicada por
11
 * la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o
12
 * (a su elección) cualquier versión posterior de la misma.
13
 *
14
 * Este programa se distribuye con la esperanza de que sea útil, pero SIN
15
 * GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD
16
 * PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública
17
 * General Affero de GNU para obtener una información más detallada.
18
 *
19
 * Debería haber recibido una copia de la Licencia Pública General Affero de GNU
20
 * junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace Derafu\Lib\Core\Package\Prime\Component\Mail\Worker\Receiver\Strategy;
26
27
use Derafu\Lib\Core\Package\Prime\Component\Mail\Abstract\AbstractMailboxStrategy;
28
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\ReceiverStrategyInterface;
29
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface;
30
31
/**
32
 * Clase para la recepción de correos utilizando IMAP.
33
 */
34
class ImapReceiverStrategy extends AbstractMailboxStrategy implements ReceiverStrategyInterface
35
{
36
    /**
37
     * Esquema de las opciones.
38
     *
39
     * @var array<string,array>
40
     */
41
    protected array $optionsSchema = [
42
        'strategy' => [
43
            'types' => 'string',
44
            'default' => 'imap',
45
        ],
46
        'transport' => [
47
            'types' => 'array',
48
            'schema' => [
49
                'host' => [
50
                    'types' => 'string',
51
                    'default' => 'imap.gmail.com',
52
                ],
53
                'port' => [
54
                    'types' => 'int',
55
                    'default' => 993,
56
                ],
57
                'encryption' => [
58
                    'types' => ['string', 'null'],
59
                    'default' => 'ssl',
60
                ],
61
                'username' => [
62
                    'types' => 'string',
63
                    'required' => true,
64
                ],
65
                'password' => [
66
                    'types' => 'string',
67
                    'required' => true,
68
                ],
69
                'mailbox' => [
70
                    'types' => 'string',
71
                    'default' => 'INBOX',
72
                ],
73
                'attachments_dir' => [
74
                    'types' => ['string', 'null'],
75
                    'default' => null,
76
                ],
77
                'verify_peer' => [
78
                    'types' => 'bool',
79
                    'default' => true,
80
                ],
81
                'dsn' => [
82
                    'types' => 'string',
83
                ],
84
                'endpoint' => [
85
                    'types' => 'string',
86
                ],
87
                'search' => [
88
                    'types' => 'array',
89
                    'schema' => [
90
                        'criteria' => [
91
                            'types' => 'string',
92
                            'default' => 'UNSEEN',
93
                        ],
94
                        'markAsSeen' => [
95
                            'types' => 'bool',
96
                            'default' => false,
97
                        ],
98
                        'attachmentFilters' => [
99
                            'types' => 'array',
100
                            'default' => [],
101
                        ],
102
                    ],
103
                ],
104
            ],
105
        ],
106
    ];
107
108
    /**
109
     * {@inheritDoc}
110
     */
111
    protected function resolveDsn(DataContainerInterface $options): string
112
    {
113
        $transportOptions = $options->get('transport');
114
115
        if (!empty($transportOptions['dsn'])) {
116
            return $transportOptions['dsn'];
117
        }
118
119
        $dsn = sprintf(
120
            '{%s:%d/imap%s%s}%s',
121
            $transportOptions['host'],
122
            $transportOptions['port'],
123
            $transportOptions['encryption'] === 'ssl' ? '/ssl' : '',
124
            (
125
                isset($transportOptions['verify_peer'])
126
                && !$transportOptions['verify_peer']
127
            )
128
                ? '/novalidate-cert'
129
                : '',
130
            $transportOptions['mailbox']
131
        );
132
133
        $options->set('transport.dsn', $dsn);
134
135
        return $dsn;
136
    }
137
}
138