Completed
Push — feature/EVO-7964_fundInfo-exte... ( aae336...bfbb61 )
by Bastian
62:45
created

ConfigurationLoaderAbstract::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * ConfigurationLoaderAbstract
4
 */
5
6
namespace Graviton\ProxyExtensionBundle\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 Graviton\ProxyBundle\Definition\Loader\LoaderInterface;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\Validator\Constraints\Url;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
/**
18
 * Class ConfigurationLoaderAbstract
19
 *
20
 * @package Graviton\ProxyExtensionBundle\Definition\Loader
21
 *
22
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
23
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link     http://swisscom.ch
25
 */
26
abstract class ConfigurationLoaderAbstract implements LoaderInterface
0 ignored issues
show
Coding Style introduced by
ConfigurationLoaderAbstract does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
{
28
    /** @var ValidatorInterface */
29
    private $validator;
30
31
    /** @var  DispersalStrategyInterface */
32
    private $strategy;
33
34
    /** @var array  */
35
    private $options = [];
36
37
38
    /**
39
     * constructor
40
     *
41
     * @param ValidatorInterface $validator validator
42
     * @param LoggerInterface    $logger    Logger
43
     */
44
    public function __construct(ValidatorInterface $validator, LoggerInterface $logger)
45
    {
46
        $this->validator = $validator;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     *
53
     * @param array $options Configuration options ['prefix']
54
     *
55
     * @return void
56
     */
57 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...
58
    {
59
        if (!empty($options['prefix'])) {
60
            $options['storeKey'] = $options['prefix'];
61
            unset($options['prefix']);
62
        }
63
64
        $this->options = array_merge($this->options, $options);
65
    }
66
67
    /**
68
     * @inheritDoc
69
     *
70
     * @param DispersalStrategyInterface $strategy Strategy to be used
71
     *
72
     * @return void
73
     */
74
    public function setDispersalStrategy(DispersalStrategyInterface $strategy)
75
    {
76
        $this->strategy = $strategy;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     *
82
     * @param CacheProvider $cache          Cache layer to be used
83
     * @param string        $cacheNamespace Name of the cache to be used
84
     * @param int           $cacheLifetime  Cache time to life
85
     *
86
     * @return void
87
     */
88
    public function setCache(CacheProvider $cache, $cacheNamespace, $cacheLifetime)
89
    {
90
    }
91
92
    /**
93
     * Determines, if the current loader is capable of handling the request.
94
     *
95
     * @param string $url Current url
96
     *
97
     * @return bool
98
     */
99
    public function supports($url)
100
    {
101
        $error = $this->validator->validate($url, [new Url()]);
102
103
        return 0 === count($error);
104
    }
105
106
    /**
107
     * @inheritDoc
108
     *
109
     * @param string $url Current Url
110
     *
111
     * @return ApiDefinition
112
     */
113
    public function load($url)
114
    {
115
        $apiDef =  new ApiDefinition();
116
        $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...
117
        $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...
118
119
        $this->defineSchema($apiDef);
120
        $apiDef->addEndpoint($this->options['endpoint']);
121
122
        return $apiDef;
123
    }
124
125
    /**
126
     * @inheritDoc
127
     *
128
     * @param ApiDefinition $apiDef Api definition the Schema to be defined in.
129
     *
130
     * @return void
131
     */
132
    private function defineSchema(ApiDefinition $apiDef)
133
    {
134
        if (array_key_exists('endpoint', $this->options)) {
135
            $finder = $this->findSchemaFile($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
    /**
154
     * Provides information about a filesystem directory and its' containing files.
155
     *
156
     * @param string $prefix Name of the directory to search in.
157
     *
158
     * @return Finder
159
     */
160
    abstract protected function findSchemaFile($prefix);
161
}
162