Completed
Push — master ( 57db8a...f718e5 )
by Tobias
06:48
created

CurlFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 47
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createClient() 0 21 5
1
<?php
2
3
namespace Http\HttplugBundle\ClientFactory;
4
5
use Http\Client\Curl\Client;
6
use Http\Message\MessageFactory;
7
use Http\Message\StreamFactory;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class CurlFactory implements ClientFactory
13
{
14
    /**
15
     * @var MessageFactory
16
     */
17
    private $messageFactory;
18
19
    /**
20
     * @var StreamFactory
21
     */
22
    private $streamFactory;
23
24
    /**
25
     * @param MessageFactory $messageFactory
26
     * @param StreamFactory  $streamFactory
27
     */
28
    public function __construct(MessageFactory $messageFactory, StreamFactory $streamFactory)
29
    {
30
        $this->messageFactory = $messageFactory;
31
        $this->streamFactory = $streamFactory;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function createClient(array $config = [])
38
    {
39
        if (!class_exists('Http\Client\Curl\Client')) {
40
            throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
41
        }
42
43
        // Try to resolve curl constant names
44
        foreach ($config as $key => $value) {
45
            // If the $key is a string we assume it is a constant
46
            if (is_string($key)) {
47
                if (null === ($constantValue = constant($key))) {
48
                    throw new \LogicException(sprintf('Key %s is not an int nor a CURL constant', $key));
49
                }
50
51
                unset($config[$key]);
52
                $config[$constantValue] = $value;
53
            }
54
        }
55
56
        return new Client($this->messageFactory, $this->streamFactory, $config);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Http\Client\...treamFactory, $config); (Http\Client\Curl\Client) is incompatible with the return type declared by the interface Http\HttplugBundle\Clien...ntFactory::createClient of type Http\Client\HttpClient.

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...
57
    }
58
}
59