Passed
Branch master (296944)
by Andreas
21:23
created

componentPass::find_builtin_components()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
    private $classmap = [];
23
24 1
    public function process(ContainerBuilder $container)
25
    {
26 1
        $paths = $this->find_builtin_components();
27
28
        // now we look for extra components the user may have registered
29 1
        foreach ($container->getParameter('midcom.midcom_components') as $path) {
30 1
            if (!file_exists($path . '/config/manifest.inc')) {
31
                throw new midcom_error('No manifest found in path ' . $path);
32
            }
33 1
            $paths[] = $path . '/config/manifest.inc';
34
        }
35
36 1
        foreach ($paths as $path) {
37 1
            $manifest = new midcom_core_manifest($path);
38 1
            $components[$manifest->name] = $path;
39 1
            if ($manifest->watches !== null) {
40 1
                $this->add_watches($manifest->name, $manifest->watches);
41
            }
42
43 1
            $this->process_manifest($manifest, $container);
44
        }
45
46 1
        $cl = $container->getDefinition('componentloader');
47 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 36. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
49 1
        $watcher = $container->getDefinition('watcher');
50 1
        $watcher->addArgument($this->watches);
51
52 1
        $dbclassloader = $container->getDefinition('dbclassloader');
53 1
        $dbclassloader->addArgument($this->classmap);
54
    }
55
56 1
    private function find_builtin_components() : array
57
    {
58 1
        $components = [];
59 1
        $finder = (new Finder())
60 1
            ->files()
61 1
            ->in([MIDCOM_ROOT, dirname(MIDCOM_ROOT) . '/src'])
62 1
            ->name('manifest.inc');
63 1
        foreach ($finder as $file) {
64 1
            $components[] = $file->getPathname();
65
        }
66 1
        return $components;
67
    }
68
69 1
    private function add_watches(string $component, array $watches)
70
    {
71 1
        foreach ($watches as $watch) {
72 1
            foreach (array_keys($this->watches) as $operation_id) {
73
                // Check whether the operations flag list from the component
74
                // contains the operation_id we're checking a watch for.
75 1
                if ($watch['operations'] & $operation_id) {
76 1
                    $this->watches[$operation_id][] = [
77 1
                        $component => $watch['classes']
78
                    ];
79
                }
80
            }
81
        }
82
    }
83
84
    /**
85
     * Register manifest data.
86
     *
87
     * All default privileges are made known to ACL, the watches are registered
88
     */
89 1
    private function process_manifest(midcom_core_manifest $manifest, ContainerBuilder $container)
90
    {
91
        // Register Privileges
92 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...
93 1
            $acl = $container->getDefinition('auth.acl');
94 1
            $acl->addMethodCall('register_default_privileges', [$manifest->privileges]);
95
        }
96 1
        $this->classmap[$manifest->name] = $manifest->class_mapping;
97 1
        if ($manifest->name == 'midcom') {
98 1
            $this->classmap['midcom'][$container->getParameter('midcom.person_class')] = \midcom_db_person::class;
99
        }
100
    }
101
}