Passed
Pull Request — master (#134)
by Arman
03:44
created

MailerManager::getHandler()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 21
rs 9.9332
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.0
13
 */
14
15
namespace Quantum\Libraries\Mailer;
16
17
use Quantum\Exceptions\ConfigException;
18
use Quantum\Exceptions\MailerException;
19
use Quantum\Exceptions\LangException;
20
use Quantum\Exceptions\AppException;
21
use Quantum\Exceptions\DiException;
22
use Quantum\Loader\Setup;
23
use ReflectionException;
24
25
/**
26
 * class MailerManager
27
 * @package Quantum\Libraries\Mailer
28
 */
29
class MailerManager
30
{
31
32
    /**
33
     * Available mail adapters
34
     */
35
    const ADAPTERS = [
36
        'smtp',
37
        'sendinblue',
38
        'sendgrid',
39
        'mandrill',
40
        'mailgun',
41
    ];
42
43
    /**
44
     * @var MailerInterface
45
     */
46
    private static $adapter;
47
48
    /**
49
     * Get Handler
50
     * @return MailerInterface
51
     * @throws ConfigException
52
     * @throws DiException
53
     * @throws MailerException
54
     * @throws ReflectionException
55
     * @throws LangException
56
     */
57
    public static function getHandler(): MailerInterface
58
    {
59
        if (self::$adapter !== null) {
60
            return self::$adapter;
61
        }
62
63
        if (!config()->has('mailer')) {
64
            config()->import(new Setup('config', 'mailer'));
65
        }
66
67
        $mailerAdapter = config()->get('mailer.current');
68
69
        if (!in_array($mailerAdapter, self::ADAPTERS)) {
70
            throw MailerException::unsupportedAdapter($mailerAdapter);
0 ignored issues
show
Bug introduced by
It seems like $mailerAdapter can also be of type null; however, parameter $name of Quantum\Exceptions\Maile...n::unsupportedAdapter() 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

70
            throw MailerException::unsupportedAdapter(/** @scrutinizer ignore-type */ $mailerAdapter);
Loading history...
71
        }
72
73
        $currentMailer = config()->get('mailer.current');
74
75
        $mailerAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($currentMailer) . 'Adapter';
0 ignored issues
show
Bug introduced by
It seems like $currentMailer can also be of type null; however, parameter $string of ucfirst() 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

75
        $mailerAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $currentMailer) . 'Adapter';
Loading history...
76
77
        return self::$adapter = $mailerAdapterClassName::getInstance(config()->get('mailer.' . $currentMailer));
78
    }
79
80
    /**
81
     * @param string $method
82
     * @param array|null $arguments
83
     * @return mixed
84
     * @throws AppException
85
     */
86
    public function __call(string $method, ?array $arguments)
87
    {
88
        if (!method_exists(self::$adapter, $method)) {
89
            throw AppException::methodNotSupported($method, get_class(self::$adapter));
90
        }
91
92
        return self::$adapter->$method(...$arguments);
93
    }
94
95
}
96