ServerFactory::make()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
ccs 21
cts 21
cp 1
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Innmind\RestBundle\Client;
4
5
use Innmind\RestBundle\Client\Server\CapabilitiesFactory;
6
use Innmind\Rest\Client\Client;
7
use Innmind\Rest\Client\Validator;
8
use Innmind\Rest\Client\Serializer\Normalizer\ResourceNormalizer;
9
use Innmind\UrlResolver\ResolverInterface;
10
use Symfony\Component\Serializer\SerializerInterface;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use GuzzleHttp\Client as Http;
14
15
class ServerFactory
16
{
17
    protected $instances = [];
18
    protected $resolver;
19
    protected $capabilities;
20
    protected $loader;
21
    protected $serializer;
22
    protected $validator;
23
    protected $dispatcher;
24
    protected $http;
25
26 2
    public function __construct(
27
        ResolverInterface $resolver,
28
        CapabilitiesFactory $capabilities,
29
        LoaderFactory $loader,
30
        SerializerInterface $serializer,
31
        ValidatorInterface $validator,
32
        EventDispatcherInterface $dispatcher,
33
        Http $http
34
    ) {
35 2
        $this->resolver = $resolver;
36 2
        $this->capabilities = $capabilities;
37 2
        $this->loader = $loader;
38 2
        $this->serializer = $serializer;
39 2
        $this->validator = $validator;
40 2
        $this->dispatcher = $dispatcher;
41 2
        $this->http = $http;
42 2
    }
43
44
    /**
45
     * Make a server object for the given host
46
     *
47
     * @param string $host
48
     *
49
     * @return Server
50
     */
51 2
    public function make($host)
52
    {
53 2
        $host = $this->resolver->resolve($host, '/');
54 2
        $hash = md5($host);
55
56 2
        if (isset($this->instances[$hash])) {
57 2
            return $this->instances[$hash];
58
        }
59
60 2
        $validator = new Validator(
61 2
            $this->validator,
62
            new ResourceNormalizer
63 2
        );
64 2
        $instance = new Server(
65 2
            $this->capabilities->make($host),
66 2
            new Client(
67 2
                $this->loader->make($host),
68 2
                $this->serializer,
69 2
                $this->resolver,
70 2
                $validator,
71 2
                $this->dispatcher,
72 2
                $this->http
73 2
            )
74 2
        );
75 2
        $this->instances[$hash] = $instance;
76
77 2
        return $instance;
78
    }
79
}
80