Completed
Push — master ( 613d98...fa04c9 )
by Rémi
20:40
created

DriverFactory::getDriver()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
cc 8
eloc 12
nc 6
nop 1
1
<?php
2
3
namespace Burrow\Driver;
4
5
use Burrow\Driver;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use PhpAmqpLib\Connection\AMQPLazyConnection;
8
9
class DriverFactory
10
{
11
    /**
12
     * Return the driver for your system
13
     *
14
     * It accepts a PECL connection, a PhpAmqpLib connection or an array following
15
     * the following schema :
16
     * [
17
     *     'host' => '<hostValue>',
18
     *     'port' => '<portValue>',
19
     *     'user' => '<userValue>',
20
     *     'pwd'  => '<pwdValue>'
21
     * ]
22
     *
23
     * If you provide an array, the PECL extension has precedence over the PhpAmqpLib.
24
     *
25
     * @param $connection
26
     *
27
     * @return Driver
28
     */
29
    public static function getDriver($connection)
30
    {
31
        if (is_array($connection) &&
32
            isset($connection['host']) &&
33
            isset($connection['port']) &&
34
            isset($connection['user']) &&
35
            isset($connection['pwd'])
36
        ) {
37
            $connection = self::getConnectionFromArray($connection);
38
        }
39
40
        if ($connection instanceof AbstractConnection) {
41
            return new PhpAmqpLibDriver($connection);
42
        }
43
44
        if ($connection instanceof \AMQPConnection) {
45
            return new PeclAmqpDriver($connection);
46
        }
47
48
        throw new \InvalidArgumentException('You provided an unsupported connection');
49
    }
50
51
    /**
52
     * @param $connection
53
     *
54
     * @return \AMQPConnection | AMQPLazyConnection
55
     */
56
    private static function getConnectionFromArray($connection)
57
    {
58
        if (static::peclExtensionIsAvailable()) {
59
            return static::getPeclConnection($connection);
60
        }
61
62
        return static::getPhpAmqpLibConnection($connection);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return static::getPhpAmq...onnection($connection); (PhpAmqpLib\Connection\AMQPLazyConnection) is incompatible with the return type documented by Burrow\Driver\DriverFact...:getConnectionFromArray of type AMQPConnection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
    }
64
65
    /**
66
     * @param string[] $connection
67
     *
68
     * @return \AMQPConnection
69
     *
70
     * @codeCoverageIgnore
71
     */
72
    protected static function getPeclConnection(array $connection)
73
    {
74
        $amqpConnection = new \AMQPConnection();
75
        $amqpConnection->setHost($connection['host']);
76
        $amqpConnection->setPort($connection['port']);
77
        $amqpConnection->setLogin($connection['user']);
78
        $amqpConnection->setPassword($connection['pwd']);
79
80
        return $amqpConnection;
81
    }
82
83
    /**
84
     * @param string[] $connection
85
     *
86
     * @return AMQPLazyConnection
87
     *
88
     * @codeCoverageIgnore
89
     */
90
    protected static function getPhpAmqpLibConnection(array $connection)
91
    {
92
        return new AMQPLazyConnection(
93
            $connection['host'],
94
            $connection['port'],
95
            $connection['user'],
96
            $connection['pwd']
97
        );
98
    }
99
100
    /**
101
     * @return bool
102
     *
103
     * @codeCoverageIgnore
104
     */
105
    protected static function peclExtensionIsAvailable()
106
    {
107
        return class_exists(\AMQPConnection::class);
108
    }
109
}
110