Completed
Pull Request — develop (#512)
by Bastian
08:49
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
 * ConfigurationLoader
4
 */
5
6
namespace Graviton\ProxyBundle\Definition\Loader;
7
8
use Doctrine\Common\Cache\CacheProvider;
9
use Graviton\ProxyBundle\Definition\ApiDefinition;
10
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\DispersalStrategyInterface;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Validator\Constraints\Url;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
/**
17
 * Class ConfigurationLoader
18
 *
19
 * @package Graviton\ProxyBundle\Definition\Loader
20
 *
21
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
22
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
23
 * @link     http://swisscom.ch
24
 */
25
class ConfigurationLoader implements LoaderInterface
26
{
27
    /** @var ValidatorInterface */
28
    private $validator;
29
30
    /** @var  DispersalStrategyInterface */
31
    private $strategy;
32
33
    /** @var array  */
34
    private $options = [];
35
36
37
    /**
38
     * constructor
39
     *
40
     * @param ValidatorInterface $validator validator
41
     * @param LoggerInterface    $logger    Logger
42
     */
43
    public function __construct(ValidatorInterface $validator, LoggerInterface $logger)
44
    {
45
        $this->validator = $validator;
46
        $this->logger = $logger;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     *
52
     * @param array $options Configuration options ['prefix']
53
     *
54
     * @return void
55
     */
56 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...
57
    {
58
        if (!empty($options['prefix'])) {
59
            $options['storeKey'] = $options['prefix'];
60
            unset($options['prefix']);
61
        }
62
63
        $this->options = array_merge($this->options, $options);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     *
69
     * @param DispersalStrategyInterface $strategy Strategy to be used
70
     *
71
     * @return void
72
     */
73
    public function setDispersalStrategy(DispersalStrategyInterface $strategy)
74
    {
75
        $this->strategy = $strategy;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     *
81
     * @param CacheProvider $cache          Cache layer to be used
82
     * @param string        $cacheNamespace Name of the cache to be used
83
     * @param int           $cacheLifetime  Cache time to life
84
     *
85
     * @return void
86
     */
87
    public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime)
88
    {
89
    }
90
91
    /**
92
     * Determines, if the current loader is capable of handling the request.
93
     *
94
     * @param string $url Current url
95
     *
96
     * @return bool
97
     */
98
    public function supports($url)
99
    {
100
        $error = $this->validator->validate($url, [new Url()]);
101
102
        return 0 === count($error);
103
    }
104
105
    /**
106
     * @inheritDoc
107
     *
108
     * @param string $url Current Url
109
     *
110
     * @return ApiDefinition
111
     */
112
    public function load($url)
113
    {
114
        $apiDef =  new ApiDefinition();
115
        $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...
116
        $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...
117
118
        $this->defineSchema($apiDef);
119
        $apiDef->addEndpoint($this->options['endpoint'] . '/');
120
121
        return $apiDef;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     *
127
     * @param ApiDefinition $apiDef Api definition the Schema to be defined in.
128
     *
129
     * @return void
130
     */
131
    private function defineSchema(ApiDefinition $apiDef)
132
    {
133
        if (array_key_exists('endpoint', $this->options)) {
134
            $finder = new Finder();
135
            $finder->files()->in(__DIR__ .'/../../Resources/schema/'. $this->options['storeKey']);
136
137
            foreach ($finder as $file) {
138
                $endpoint = $this->options['endpoint'];
139
140
                // MAGIC happens here:
141
                // need to streamline endpoint and filename to be able to find the endpoint.
142
                $cmp = str_replace('/', '', $endpoint);
143
                list($filename, ) = explode('.', $file->getFilename());
144
145
                if ($cmp == $filename) {
146
                    $schema = json_decode(file_get_contents($file->getRealPath()));
147
                    $apiDef->addSchema($endpoint, $schema);
148
                }
149
            }
150
        }
151
    }
152
}
153