CapabilitiesFactory::make()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 28
loc 28
ccs 20
cts 20
cp 1
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Innmind\RestBundle\Client\Server;
4
5
use Innmind\RestBundle\Client\Server\Cache\FileCache;
6
use Innmind\RestBundle\Client\LoaderFactory;
7
use Innmind\UrlResolver\ResolverInterface;
8
use GuzzleHttp\Client as Http;
9
10
class CapabilitiesFactory
11
{
12
    protected $instances = [];
13
    protected $cacheDir;
14
    protected $resolver;
15
    protected $http;
16
    protected $loader;
17
18 6
    public function __construct(
19
        $cacheDir,
20
        ResolverInterface $resolver,
21
        Http $http,
22
        LoaderFactory $loader
23
    ) {
24 6
        $this->cacheDir = (string) $cacheDir;
25 6
        $this->resolver = $resolver;
26 6
        $this->http = $http;
27 6
        $this->loader = $loader;
28 6
    }
29
30
    /**
31
     * Make a capabilities object for a given host
32
     *
33
     * @param string $host
34
     *
35
     * @return Capabilities
36
     */
37 6 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...
38
    {
39 6
        $host = $this->resolver->resolve($host, '/');
40 6
        $hash = md5($host);
41
42 6
        if (isset($this->instances[$hash])) {
43 2
            return $this->instances[$hash];
44
        }
45
46 6
        $dir = sprintf(
47 6
            '%s/%s',
48 6
            rtrim($this->cacheDir, '/'),
49
            $hash
50 6
        );
51 6
        $instance = new Capabilities(
52 6
            $host,
53 6
            $this->http,
54 6
            new FileCache(sprintf(
55 6
                '%s/capabilities.php',
56
                $dir
57 6
            )),
58 6
            $this->loader->make($host),
59 6
            $this->resolver
60 6
        );
61 6
        $this->instances[$hash] = $instance;
62
63 6
        return $instance;
64
    }
65
}
66