Extractor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A extract() 0 8 2
B getCurrent() 0 17 6
A serialize() 0 4 1
A unserialize() 0 4 1
1
<?php
2
3
namespace Knp\MegaMAN\Extractor;
4
5
use Knp\MegaMAN\Extractor as ExtractorInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class Extractor implements ExtractorInterface, \Serializable
9
{
10
    /**
11
     * @var string
12
     */
13
    private $cache;
14
15
    /**
16
     * @var RequestStack
17
     */
18
    private $stack;
19
20
    /**
21
     * @var mixed
22
     */
23
    private $json;
24
25
    /**
26
     * @param string       $cache
27
     * @param RequestStack $stack
28
     */
29
    public function __construct($cache, RequestStack $stack)
30
    {
31
        $this->cache = $cache;
32
        $this->stack = $stack;
33
34
        $this->extract();
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function extract()
41
    {
42
        if (null !== $this->json) {
43
            return $this->json;
44
        }
45
46
        return $this->json = json_decode(file_get_contents($this->cache), true);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getCurrent()
53
    {
54
        $dependencies = $this->extract();
55
        $current      = $this->stack->getMasterRequest()->get('package');
56
57
        if (null === $current) {
58
            foreach ($dependencies as $key => $dependency) {
59
                if (true === array_key_exists('local', $dependency) && true === $dependency['local']) {
60
                    $current = $key;
61
                }
62
            }
63
        }
64
65
        if (true === array_key_exists($current, $dependencies)) {
66
            return $dependencies[$current];
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function serialize()
74
    {
75
        return serialize([$this->json, $this->stack]);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function unserialize($value)
82
    {
83
        list($this->json, $this->stack) = unserialize($value);
84
    }
85
}
86