LoaderFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4286
cc 1
eloc 10
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Innmind\RestBundle\Client;
4
5
use Innmind\Rest\Client\Definition\Loader;
6
use Innmind\Rest\Client\Definition\Builder;
7
use Innmind\Rest\Client\Cache\FileCache;
8
use Innmind\UrlResolver\ResolverInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
use GuzzleHttp\Client as Http;
11
12
class LoaderFactory
13
{
14
    protected $instances = [];
15
    protected $cacheDir;
16
    protected $resolver;
17
    protected $builder;
18
    protected $http;
19
    protected $validator;
20
21 8
    public function __construct(
22
        $cacheDir,
23
        ResolverInterface $resolver,
24
        Http $http,
25
        ValidatorInterface $validator
26
    ) {
27 8
        $this->cacheDir = (string) $cacheDir;
28 8
        $this->resolver = $resolver;
29 8
        $this->http = $http;
30 8
        $this->validator = $validator;
31 8
        $this->builder = new Builder;
32 8
    }
33
34
    /**
35
     * Create a loader for a given host
36
     *
37
     * @param string $host
38
     *
39
     * @return Loader
40
     */
41 8 View Code Duplication
    public function make($host)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 8
        $host = $this->resolver->resolve($host, '/');
44 8
        $hash = md5($host);
45
46 8
        if (isset($this->instances[$hash])) {
47 4
            return $this->instances[$hash];
48
        }
49
50 8
        $dir = sprintf(
51 8
            '%s/%s',
52 8
            rtrim($this->cacheDir, '/'),
53
            $hash
54 8
        );
55 8
        $instance = new Loader(
56 8
            new FileCache(sprintf(
57 8
                '%s/definitions.php',
58
                $dir
59 8
            )),
60 8
            $this->resolver,
61 8
            $this->builder,
62 8
            $this->http,
63 8
            $this->validator
64 8
        );
65 8
        $this->instances[$hash] = $instance;
66
67 8
        return $instance;
68
    }
69
}
70