SmtpSenderStrategy::resolveEndpoint()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 27
ccs 0
cts 14
cp 0
crap 42
rs 9.2222
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\Sender\Strategy;
26
27
use Derafu\Lib\Core\Package\Prime\Component\Mail\Abstract\AbstractMailerStrategy;
28
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\SenderStrategyInterface;
29
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface;
30
31
/**
32
 * Estrategia para el envío de correos electrónicos utilizando SMTP.
33
 */
34
class SmtpSenderStrategy extends AbstractMailerStrategy implements SenderStrategyInterface
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' => 'smtp',
45
        ],
46
        'transport' => [
47
            'types' => 'array',
48
            'schema' => [
49
                'host' => [
50
                    'types' => 'string',
51
                    'default' => 'smtp.gmail.com',
52
                ],
53
                'port' => [
54
                    'types' => 'int',
55
                    'default' => 465,
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
                'verify_peer' => [
70
                    'types' => 'bool',
71
                    'default' => true,
72
                ],
73
                'dsn' => [
74
                    'types' => 'string',
75
                ],
76
                'endpoint' => [
77
                    'types' => 'string',
78
                ],
79
            ],
80
        ],
81
    ];
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    protected function resolveDsn(DataContainerInterface $options): string
87
    {
88
        $transportOptions = $options->get('transport');
89
90
        if (!empty($transportOptions['dsn'])) {
91
            return $transportOptions['dsn'];
92
        }
93
94
        $dsn = sprintf(
95
            'smtp://%s:%s@%s:%d?encryption=%s&verify_peer=%d',
96
            $transportOptions['username'],
97
            $transportOptions['password'],
98
            $transportOptions['host'],
99
            $transportOptions['port'],
100
            (string) $transportOptions['encryption'],
101
            (int) $transportOptions['verify_peer'],
102
        );
103
104
        $options->set('transport.dsn', $dsn);
105
106
        return $dsn;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    protected function resolveEndpoint(DataContainerInterface $options): string
113
    {
114
        $transportOptions = $options->get('transport');
115
116
        if (!empty($transportOptions['endpoint'])) {
117
            return $transportOptions['endpoint'];
118
        }
119
120
        $endpoint = '';
121
122
        if (isset($options['encryption'])) {
123
            $endpoint .= $options['encryption'] . '://';
124
        }
125
126
        $endpoint .= $options['host'];
127
128
        if (isset($options['port'])) {
129
            $endpoint .= ':' . $options['port'];
130
        }
131
132
        if (isset($options['verify_peer']) && !$options['verify_peer']) {
133
            $endpoint .= '/novalidate-cert';
134
        }
135
136
        $options->set('transport.endpoint', $endpoint);
137
138
        return $endpoint;
139
    }
140
}
141