MailerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMailer() 0 9 3
A create() 0 11 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\Mail\Wrapper\MailWrapperInterface;
13
use ByJG\Util\Uri;
14
15
class MailerFactory
16
{
17
    private static $config = [];
18
19
    /**
20
     * @param string $protocol
21
     * @param string $class
22
     * @throws \ByJG\Mail\Exception\InvalidMailHandlerException
23
     */
24 4
    public static function registerMailer($class)
25
    {
26 4
        if (!in_array(MailWrapperInterface::class, class_implements($class))) {
27 1
            throw new InvalidMailHandlerException('Class not implements ConnectorInterface!');
28
        }
29
30 3
        $protocolList = $class::schema();
31 3
        foreach ((array)$protocolList as $item) {
32 3
            self::$config[$item] = $class;
33
        }
34
    }
35
36
37
    /**
38
     * @param $connection
39
     * @return \ByJG\Mail\Wrapper\MailWrapperInterface
40
     * @throws \ByJG\Mail\Exception\ProtocolNotRegisteredException
41
     */
42 2
    public static function create($connection)
43
    {
44 2
        $uri = new Uri($connection);
45
46 2
        if (!isset(self::$config[$uri->getScheme()])) {
47 1
            throw new ProtocolNotRegisteredException('Protocol not found/registered!');
48
        }
49
50 1
        $class = self::$config[$uri->getScheme()];
51
52 1
        return new $class($uri);
53
    }
54
}
55