Completed
Push — feature/EVO-7964_fundInfo-exte... ( 142d3d...cf9459 )
by Bastian
08:28 queued 08:21
created

ConfigurationLoader::setDispersalStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
53
     */
54 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...
55
    {
56
        if (!empty($options['prefix'])) {
57
            $options['storeKey'] = $options['prefix'];
58
            unset($options['prefix']);
59
        }
60
61
        $this->options = array_merge($this->options, $options);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     *
67
     * @param DispersalStrategyInterface $strategy
68
     */
69
    public function setDispersalStrategy(DispersalStrategyInterface $strategy)
70
    {
71
        $this->strategy = $strategy;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     *
77
     * @param CacheProvider $cache          Cache layer to be used
78
     * @param string        $cacheNamespace Name of the cache to be used
79
     * @param int           $cacheLifetime  Cache time to life
80
     */
81
    public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime)
82
    {
83
    }
84
85
    /**
86
     * Determines, if the current loader is capable of handling the request.
87
     *
88
     * @param string $url Current url
89
     *
90
     * @return bool
91
     */
92
    public function supports($url)
93
    {
94
        $error = $this->validator->validate($url, [new Url()]);
95
96
        return 0 === count($error);
97
    }
98
99
    /**
100
     * @inheritDoc
101
     *
102
     * @param string $url Current Url
103
     *
104
     * @return ApiDefinition
105
     */
106
    public function load($url)
107
    {
108
        $apiDef =  new ApiDefinition();
109
        $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...
110
        $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...
111
112
        $this->defineSchema($apiDef);
113
        $apiDef->addEndpoint($this->options['endpoint'] . '/');
114
115
        return $apiDef;
116
    }
117
118
    /**
119
     * @inheritDoc
120
     *
121
     * @param ApiDefinition $apiDef Api definition the Schema to be defined in.
122
     */
123
    private function defineSchema(ApiDefinition $apiDef)
124
    {
125
        if (array_key_exists('endpoint', $this->options)) {
126
            $finder = new Finder();
127
            $finder->files()->in(__DIR__ .'/../../Resources/schema/'. $this->options['storeKey']);
128
129
            foreach ($finder as $file) {
130
                $endpoint = $this->options['endpoint'];
131
132
                // MAGIC happens here:
133
                // need to streamline endpoint and filename to be able to find the endpoint.
134
                $cmp = str_replace('/', '', $endpoint);
135
                list($filename, ) = explode('.', $file->getFilename());
136
137
                if ($cmp == $filename) {
138
                    $schema = json_decode(file_get_contents($file->getRealPath()));
139
                    $apiDef->addSchema($endpoint, $schema);
140
                }
141
            }
142
        }
143
    }
144
}
145