SourceUrl::__invoke()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 7
nop 1
1
<?php
2
3
namespace Knp\MegaMAN\Filter;
4
5
use Knp\MegaMAN\Filter;
6
7
class SourceUrl implements Filter
8
{
9
    /**
10
     * @var string
11
     */
12
    private $installed;
13
14
    /**
15
     * @param string $installed
16
     */
17
    public function __construct($installed)
18
    {
19
        $this->installed = $installed;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function __invoke(array $array)
26
    {
27
        $result = [];
28
        $json   = json_decode(file_get_contents($this->installed), true);
29
30
        foreach ($array as $definition) {
31
            if (false === array_key_exists('package', $definition)) {
32
                continue;
33
            }
34
35
            $definition['url'] = '';
36
37
            foreach ($json as $installed) {
38
                if ($installed['name'] === $definition['package']) {
39
                    if (false === array_key_exists('source', $installed)) {
40
                        continue;
41
                    }
42
43
                    if (false === array_key_exists('url', $installed['source'])) {
44
                        continue;
45
                    }
46
47
                    $definition['url'] = $installed['source']['url'];
48
                }
49
            }
50
51
            $result[] = $definition;
52
        }
53
54
        return $result;
55
    }
56
}
57