Passed
Push — master ( 670096...d7e64a )
by Andreas
22:23
created

componentPass::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
namespace midcom\dependencyInjection;
3
4
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use midcom_config;
7
use midcom_core_manifest;
8
use midcom_error;
9
10
class componentPass implements CompilerPassInterface
11
{
12
    /**
13
     * @var midcom_config
14
     */
15
    private $config;
16
17
    /**
18
     * @var array
19
     */
20
    private $watches = [
21
        \MIDCOM_OPERATION_DBA_CREATE => [],
22
        \MIDCOM_OPERATION_DBA_UPDATE => [],
23
        \MIDCOM_OPERATION_DBA_DELETE => [],
24
        \MIDCOM_OPERATION_DBA_IMPORT => []
25
    ];
26
27 1
    public function __construct(midcom_config $config)
28
    {
29 1
        $this->config = $config;
30 1
    }
31
32 1
    public function process(ContainerBuilder $container)
33
    {
34 1
        $paths = [];
35 1
        foreach ($this->config->get('builtin_components', []) as $path) {
36 1
            $paths[] = dirname(MIDCOM_ROOT) . '/' . $path . '/config/manifest.inc';
37
        }
38
39
        // now we look for extra components the user may have registered
40 1
        foreach ($this->config->get('midcom_components', []) as $path) {
41 1
            if (!file_exists($path . '/config/manifest.inc')) {
42
                throw new midcom_error('No manifest found in path ' . $path);
43
            }
44 1
            $paths[] = $path . '/config/manifest.inc';
45
        }
46
47 1
        foreach ($paths as $path) {
48 1
            $manifest = new midcom_core_manifest($path);
49 1
            $components[$manifest->name] = $path;
50 1
            if ($manifest->watches !== null) {
51 1
                $this->add_watches($manifest->name, $manifest->watches);
52
            }
53
54 1
            $this->process_manifest($manifest, $container);
55
        }
56
57 1
        $cl = $container->getDefinition('componentloader');
58 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 47. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
59
60 1
        $watcher = $container->getDefinition('watcher');
61 1
        $watcher->addArgument($this->watches);
62 1
    }
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
}