CurlFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
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 43
ccs 0
cts 14
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
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\ClientFactory;
6
7
use Http\Client\Curl\Client;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\StreamFactoryInterface;
10
11
/**
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class CurlFactory implements ClientFactory
15
{
16
    /**
17
     * @var ResponseFactoryInterface
18
     */
19
    private $responseFactory;
20
21
    /**
22
     * @var StreamFactoryInterface
23
     */
24
    private $streamFactory;
25
26
    public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
27
    {
28
        $this->responseFactory = $responseFactory;
29
        $this->streamFactory = $streamFactory;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function createClient(array $config = [])
36
    {
37
        if (!class_exists('Http\Client\Curl\Client')) {
38
            throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
39
        }
40
41
        // Try to resolve curl constant names
42
        foreach ($config as $key => $value) {
43
            // If the $key is a string we assume it is a constant
44
            if (is_string($key)) {
45
                if (null === ($constantValue = constant($key))) {
46
                    throw new \LogicException(sprintf('Key %s is not an int nor a CURL constant', $key));
47
                }
48
49
                unset($config[$key]);
50
                $config[$constantValue] = $value;
51
            }
52
        }
53
54
        return new Client($this->responseFactory, $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...
55
    }
56
}
57