Passed
Pull Request — master (#190)
by Arman
02:53
created

MailerFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapterClass() 0 7 2
A get() 0 7 2
A createInstance() 0 11 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Mailer\Factories;
16
17
use Quantum\Libraries\Mailer\Exceptions\MailerException;
18
use Quantum\Libraries\Config\Exceptions\ConfigException;
19
use Quantum\Libraries\Mailer\Adapters\SendinblueAdapter;
20
use Quantum\Libraries\Mailer\Adapters\SendgridAdapter;
21
use Quantum\Libraries\Mailer\Adapters\MandrillAdapter;
22
use Quantum\Libraries\Mailer\Adapters\MailgunAdapter;
23
use Quantum\Libraries\Mailer\Adapters\SmtpAdapter;
24
use Quantum\Di\Exceptions\DiException;
25
use Quantum\Exceptions\BaseException;
26
use Quantum\Libraries\Mailer\Mailer;
27
use Quantum\Loader\Setup;
28
use ReflectionException;
29
30
/**
31
 * class MailerFactory
32
 * @package Quantum\Libraries\Mailer
33
 */
34
class MailerFactory
35
{
36
37
    /**
38
     * Supported adapters
39
     */
40
    const ADAPTERS = [
41
        Mailer::SMTP => SmtpAdapter::class,
42
        Mailer::MAILGUN => MailgunAdapter::class,
43
        Mailer::MANDRILL => MandrillAdapter::class,
44
        Mailer::SENDGRID => SendgridAdapter::class,
45
        Mailer::SENDINBLUE => SendinblueAdapter::class,
46
    ];
47
48
    /**
49
     * @var Mailer|null
50
     */
51
    private static $instance = null;
52
53
    /**
54
     * @return Mailer
55
     * @throws BaseException
56
     * @throws ConfigException
57
     * @throws DiException
58
     * @throws ReflectionException
59
     */
60
    public static function get(): Mailer
61
    {
62
        if (self::$instance === null) {
63
            return self::$instance = self::createInstance();
64
        }
65
66
        return self::$instance;
67
    }
68
69
    /**
70
     * @return Mailer
71
     * @throws BaseException
72
     * @throws ConfigException
73
     * @throws DiException
74
     * @throws ReflectionException
75
     */
76
    private static function createInstance(): Mailer
77
    {
78
        if (!config()->has('mailer')) {
79
            config()->import(new Setup('config', 'mailer'));
80
        }
81
82
        $adapter = config()->get('mailer.current');
83
84
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Mailer...tory::getAdapterClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
85
86
        return new Mailer(new $adapterClass(config()->get('mailer.' . $adapter)));
87
    }
88
89
    /**
90
     * @param string $adapter
91
     * @return string
92
     * @throws BaseException
93
     */
94
    private static function getAdapterClass(string $adapter): string
95
    {
96
        if (!array_key_exists($adapter, self::ADAPTERS)) {
97
            throw MailerException::adapterNotSupported($adapter);
98
        }
99
100
        return self::ADAPTERS[$adapter];
101
    }
102
}