Completed
Push — feature/EVO-7964_fundInfo-exte... ( d593d5...64d15e )
by Bastian
11:22
created

ConfigurationLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 8.49 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 9
loc 106
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setOptions() 9 9 2
A setDispersalStrategy() 0 4 1
A setCache() 0 3 1
A supports() 0 6 1
A load() 0 11 1
A defineSchema() 0 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Graviton\ProxyBundle\Definition\Loader;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
use Graviton\ProxyBundle\Definition\ApiDefinition;
7
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\DispersalStrategyInterface;
8
use Psr\Log\LoggerInterface;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Validator\Constraints\Url;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
13
/**
14
 * Class ConfigurationLoader
15
 *
16
 * @package Graviton\ProxyBundle\Definition\Loader
17
 */
18
class ConfigurationLoader implements LoaderInterface
19
{
20
    /** @var ValidatorInterface */
21
    private $validator;
22
23
    /** @var  DispersalStrategyInterface */
24
    private $strategy;
25
26
    /** @var array  */
27
    private $options = [];
28
29
30
    /**
31
     * constructor
32
     *
33
     * @param ValidatorInterface $validator validator
34
     * @param LoggerInterface    $logger    Logger
35
     */
36
    public function __construct(ValidatorInterface $validator, LoggerInterface $logger)
37
    {
38
        $this->validator = $validator;
39
        $this->logger = $logger;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 View Code Duplication
    public function setOptions($options)
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...
46
    {
47
        if (!empty($options['prefix'])) {
48
            $options['storeKey'] = $options['prefix'];
49
            unset($options['prefix']);
50
        }
51
52
        $this->options = array_merge($this->options, $options);
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function setDispersalStrategy(DispersalStrategyInterface $strategy)
59
    {
60
        $this->strategy = $strategy;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime)
67
    {
68
    }
69
70
    /**
71
     * Determines, if the current loader is capable of handling the request.
72
     *
73
     * @param string $url
74
     *
75
     * @return bool
76
     */
77
    public function supports($url)
78
    {
79
        $error = $this->validator->validate($url, [new Url()]);
80
81
        return 0 === count($error);
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function load($url)
88
    {
89
        $apiDef =  new ApiDefinition();
90
        $apiDef->setHost(parse_url($url, PHP_URL_HOST));
0 ignored issues
show
Security Bug introduced by
It seems like parse_url($url, PHP_URL_HOST) targeting parse_url() can also be of type false; however, Graviton\ProxyBundle\Def...piDefinition::setHost() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
91
        $apiDef->setBasePath(parse_url($url, PHP_URL_PATH));
0 ignored issues
show
Security Bug introduced by
It seems like parse_url($url, PHP_URL_PATH) targeting parse_url() can also be of type false; however, Graviton\ProxyBundle\Def...finition::setBasePath() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
92
93
        $this->defineSchema($apiDef);
94
        $apiDef->addEndpoint($this->options['endpoint'] . '/');
95
96
        return $apiDef;
97
    }
98
99
    /**
100
     * @param ApiDefinition $apiDef
101
     */
102
    private function defineSchema(ApiDefinition $apiDef)
103
    {
104
        if (array_key_exists('endpoint', $this->options)) {
105
            $finder = new Finder();
106
            $finder->files()->in(__DIR__ .'/../../Resources/schema/'. $this->options['storeKey']);
107
108
            foreach ($finder as $file) {
109
                $endpoint = $this->options['endpoint'];
110
111
                // MAGIC happens here:
112
                // need to streamline endpoint and filename to be able to find the endpoint.
113
                $cmp = str_replace('/', '', $endpoint);
114
                list($filename, ) = explode('.', $file->getFilename());
115
116
                if ($cmp == $filename) {
117
                    $schema = json_decode(file_get_contents($file->getRealPath()));
118
                    $apiDef->addSchema($endpoint, $schema);
119
                }
120
            }
121
        }
122
    }
123
}
124