Completed
Push — master ( 7a9e86...009508 )
by Andreas
18:36
created

componentPass   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 80
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add_watches() 0 9 4
A find_builtin_components() 0 11 2
A process() 0 27 5
A process_manifest() 0 6 2
1
<?php
2
namespace midcom\bundle\dependencyInjection;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use midcom_core_manifest;
6
use midcom_error;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\Finder\Finder;
9
10
class componentPass implements CompilerPassInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $watches = [
16
        \MIDCOM_OPERATION_DBA_CREATE => [],
17
        \MIDCOM_OPERATION_DBA_UPDATE => [],
18
        \MIDCOM_OPERATION_DBA_DELETE => [],
19
        \MIDCOM_OPERATION_DBA_IMPORT => []
20
    ];
21
22 1
    public function process(ContainerBuilder $container)
23
    {
24 1
        $paths = $this->find_builtin_components();
25
26
        // now we look for extra components the user may have registered
27 1
        foreach ($container->getParameter('midcom.midcom_components') as $path) {
28 1
            if (!file_exists($path . '/config/manifest.inc')) {
29
                throw new midcom_error('No manifest found in path ' . $path);
30
            }
31 1
            $paths[] = $path . '/config/manifest.inc';
32
        }
33
34 1
        foreach ($paths as $path) {
35 1
            $manifest = new midcom_core_manifest($path);
36 1
            $components[$manifest->name] = $path;
37 1
            if ($manifest->watches !== null) {
38 1
                $this->add_watches($manifest->name, $manifest->watches);
39
            }
40
41 1
            $this->process_manifest($manifest, $container);
42
        }
43
44 1
        $cl = $container->getDefinition('componentloader');
45 1
        $cl->addArgument($components);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $components seems to be defined by a foreach iteration on line 34. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
46
47 1
        $watcher = $container->getDefinition('watcher');
48 1
        $watcher->addArgument($this->watches);
49 1
    }
50
51 1
    private function find_builtin_components() : array
52
    {
53 1
        $components = [];
54 1
        $finder = (new Finder())
55 1
            ->files()
56 1
            ->in([MIDCOM_ROOT, dirname(MIDCOM_ROOT) . '/src'])
57 1
            ->name('manifest.inc');
58 1
        foreach ($finder as $file) {
59 1
            $components[] = $file->getPathname();
60
        }
61 1
        return $components;
62
    }
63
64 1
    private function add_watches(string $component, array $watches)
65
    {
66 1
        foreach ($watches as $watch) {
67 1
            foreach (array_keys($this->watches) as $operation_id) {
68
                // Check whether the operations flag list from the component
69
                // contains the operation_id we're checking a watch for.
70 1
                if ($watch['operations'] & $operation_id) {
71 1
                    $this->watches[$operation_id][] = [
72 1
                        $component => $watch['classes']
73
                    ];
74
                }
75
            }
76
        }
77 1
    }
78
79
    /**
80
     * Register manifest data.
81
     *
82
     * All default privileges are made known to ACL, the watches are registered
83
     */
84 1
    private function process_manifest(midcom_core_manifest $manifest, ContainerBuilder $container)
85
    {
86
        // Register Privileges
87 1
        if ($manifest->privileges) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $manifest->privileges of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88 1
            $acl = $container->getDefinition('auth.acl');
89 1
            $acl->addMethodCall('register_default_privileges', [$manifest->privileges]);
90
        }
91
    }
92
}