Passed
Push — master ( 7ea105...f7c21b )
by Joao
39s
created

MailerFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * User: jg
4
 * Date: 28/05/17
5
 * Time: 11:50
6
 */
7
8
namespace ByJG\Mail;
9
10
use ByJG\Mail\Exception\InvalidMailHandlerException;
11
use ByJG\Mail\Exception\ProtocolNotRegisteredException;
12
use ByJG\Util\Uri;
13
14
class MailerFactory
15
{
16
    private static $config = [];
17
18
    /**
19
     * @param string $protocol
20
     * @param string $class
21
     * @throws \ByJG\Mail\Exception\InvalidMailHandlerException
22
     */
23 4
    public static function registerMailer($protocol, $class)
24
    {
25 4
        if (!class_exists($class, true)) {
26 1
            throw new InvalidMailHandlerException('Class not found!');
27
        }
28 3
        self::$config[$protocol] = $class;
29 3
    }
30
31
    /**
32
     * @param $connection
33
     * @return mixed
34
     * @throws \ByJG\Mail\Exception\ProtocolNotRegisteredException
35
     */
36 2
    public static function create($connection)
37
    {
38 2
        $uri = new Uri($connection);
39
40 2
        if (!isset(self::$config[$uri->getScheme()])) {
41 1
            throw new ProtocolNotRegisteredException('Protocol not found/registered!');
42
        }
43
44 1
        $class = self::$config[$uri->getScheme()];
45
46 1
        return new $class($uri);
47
    }
48
}
49