Completed
Push — master ( 66a40e...8e7136 )
by David
02:31 queued 12s
created

SymfonyFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 34
ccs 0
cts 12
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 8 2
1
<?php
2
3
namespace Http\HttplugBundle\ClientFactory;
4
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Psr\Http\Message\StreamFactoryInterface;
7
use Symfony\Component\HttpClient\HttpClient;
8
use Symfony\Component\HttpClient\HttplugClient;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class SymfonyFactory implements ClientFactory
14
{
15
    /**
16
     * @var ResponseFactoryInterface
17
     */
18
    private $responseFactory;
19
20
    /**
21
     * @var StreamFactoryInterface
22
     */
23
    private $streamFactory;
24
25
    /**
26
     * @param ResponseFactoryInterface $responseFactory
27
     * @param StreamFactoryInterface   $streamFactory
28
     */
29
    public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
30
    {
31
        $this->responseFactory = $responseFactory;
32
        $this->streamFactory = $streamFactory;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function createClient(array $config = [])
39
    {
40
        if (!class_exists(HttplugClient::class)) {
41
            throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.');
42
        }
43
44
        return new HttplugClient(HttpClient::create($config), $this->responseFactory, $this->streamFactory);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp... $this->streamFactory); (Symfony\Component\HttpClient\HttplugClient) 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...
45
    }
46
}
47