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

ConfigurationLoader::defineSchema()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 20
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