Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

Context   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 4
dl 0
loc 271
rs 9.3999
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAvailableConfigs() 0 8 2
A addAvailableConfig() 0 10 3
A setAvailableConfigs() 0 6 1
A loadConfigsFromPath() 0 11 3
A getConfigurationForValue() 0 14 4
A getMetaForValue() 0 4 1
A isSupported() 0 8 3
A addNewConfig() 0 18 3
A setCurrentPage() 0 6 1
A getCurrentPage() 0 4 1
A registerMeta() 0 16 3
A getRegisteredMeta() 0 4 1
A offsetSet() 0 8 2
A offsetExists() 0 4 1
A offsetUnset() 0 7 1
A offsetGet() 0 8 2
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Gimme\Context;
16
17
use Doctrine\Common\Cache\Cache;
18
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\Yaml\Parser;
21
22
class Context implements \ArrayAccess
23
{
24
    /**
25
     * Array with current page information's.
26
     *
27
     * @var Meta
28
     */
29
    protected $currentPage;
30
31
    /**
32
     * Array will all registered meta types.
33
     *
34
     * @var Meta[]
35
     */
36
    protected $registeredMeta = [];
37
38
    /**
39
     * Array with available meta configs.
40
     *
41
     * @var array
42
     */
43
    protected $availableConfigs = [];
44
45
    /**
46
     * @var Cache
47
     */
48
    protected $metadataCache;
49
50
    /**
51
     * @var string
52
     */
53
    protected $configsPath;
54
55
    /**
56
     * Context constructor.
57
     *
58
     * @param Cache  $metadataCache
59
     * @param string $configsPath
60
     */
61
    public function __construct(Cache $metadataCache, $configsPath = null)
62
    {
63
        $this->metadataCache = $metadataCache;
64
        $this->configsPath = $configsPath;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getAvailableConfigs()
71
    {
72
        if (0 === count($this->availableConfigs)) {
73
            $this->loadConfigsFromPath($this->configsPath);
74
        }
75
76
        return $this->availableConfigs;
77
    }
78
79
    /**
80
     * @param array $configuration
81
     *
82
     * @return bool
83
     */
84
    public function addAvailableConfig(array $configuration)
85
    {
86
        if (isset($configuration['class']) && !isset($this->availableConfigs[$configuration['class']])) {
87
            $this->availableConfigs[$configuration['class']] = $configuration;
88
89
            return true;
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * @param array $availableConfigs
97
     *
98
     * @return Context
99
     */
100
    public function setAvailableConfigs(array $availableConfigs)
101
    {
102
        $this->availableConfigs = $availableConfigs;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $configsPath
109
     */
110
    public function loadConfigsFromPath($configsPath)
111
    {
112
        if (file_exists($configsPath)) {
113
            $finder = new Finder();
114
            $finder->in($configsPath)->files()->name('*.yml');
115
116
            foreach ($finder as $file) {
117
                $this->addNewConfig($file->getRealPath());
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param mixed $value
124
     *
125
     * @return array
126
     *
127
     * @throws \Exception
128
     */
129
    public function getConfigurationForValue($value)
130
    {
131
        if (false === is_object($value)) {
132
            throw new \Exception('Context supports configuration loading only for objects');
133
        }
134
135
        foreach ($this->getAvailableConfigs() as $class => $configuration) {
136
            if ($value instanceof $class) {
137
                return $configuration;
138
            }
139
        }
140
141
        return [];
142
    }
143
144
    /**
145
     * @param mixed $value
146
     *
147
     * @return Meta
148
     */
149
    public function getMetaForValue($value)
150
    {
151
        return new Meta($this, $value, $this->getConfigurationForValue($value));
152
    }
153
154
    /**
155
     * @param mixed $value
156
     *
157
     * @return bool
158
     */
159
    public function isSupported($value)
160
    {
161
        if (!is_object($value)) {
162
            return false;
163
        }
164
165
        return count($this->getConfigurationForValue($value)) > 0 ? true : false;
166
    }
167
168
    /**
169
     * @param string $filePath
170
     *
171
     * @return $this
172
     */
173
    public function addNewConfig($filePath)
174
    {
175
        $cacheKey = md5($filePath);
176
        if (!$this->metadataCache->contains($cacheKey)) {
177
            if (!is_readable($filePath)) {
178
                throw new \InvalidArgumentException('Configuration file is not readable for parser');
179
            }
180
            $parser = new Parser();
181
            $configuration = $parser->parse(file_get_contents($filePath));
182
            $this->metadataCache->save($cacheKey, $configuration);
183
        } else {
184
            $configuration = $this->metadataCache->fetch($cacheKey);
185
        }
186
187
        $this->addAvailableConfig($configuration);
188
189
        return $configuration;
190
    }
191
192
    /**
193
     * Set current context page information's.
194
     *
195
     * @param Meta $currentPage
196
     *
197
     * @return self
198
     */
199
    public function setCurrentPage(Meta $currentPage)
200
    {
201
        $this->currentPage = $currentPage;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Get current context page information's.
208
     *
209
     * @return Meta
210
     */
211
    public function getCurrentPage()
212
    {
213
        return $this->currentPage;
214
    }
215
216
    /**
217
     * Register new meta type, registration is required before setting new value for meta.
218
     *
219
     * @param Meta|null $meta Meta object
220
     *
221
     * @throws \Exception if already registered
222
     *
223
     * @return bool if registered successfully
224
     */
225
    public function registerMeta(Meta $meta = null)
226
    {
227
        $configuration = $meta->getConfiguration();
0 ignored issues
show
Bug introduced by
It seems like $meta is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
228
        $name = $configuration['name'];
229
        if (!array_key_exists($name, $this->registeredMeta)) {
230
            $this->registeredMeta[$name] = $configuration;
231
232
            if (!is_null($meta)) {
233
                $this->$name = $meta;
234
            }
235
236
            return true;
237
        }
238
239
        return false;
240
    }
241
242
    /**
243
     * @return Meta[]
244
     */
245
    public function getRegisteredMeta()
246
    {
247
        return $this->registeredMeta;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function offsetSet($name, $meta)
254
    {
255
        if (in_array($name, $this->registeredMeta)) {
256
            $this->$name = $meta;
257
        }
258
259
        return true;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function offsetExists($name)
266
    {
267
        return in_array($name, $this->registeredMeta);
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function offsetUnset($name)
274
    {
275
        unset($this->registeredMeta[$name]);
276
        usent($this->$name);
277
278
        return true;
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284
    public function offsetGet($name)
285
    {
286
        if (in_array($name, $this->registeredMeta)) {
287
            return $this->$name;
288
        }
289
290
        return false;
291
    }
292
}
293