Passed
Push — master ( 31d2f3...47c5a8 )
by Andreas
09:57 queued 01:01
created

componentPass::process()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0035

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 31
ccs 18
cts 19
cp 0.9474
crap 5.0035
rs 9.3554
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        $components = [];
37 1
        foreach ($paths as $path) {
38 1
            $manifest = new midcom_core_manifest($path);
39 1
            $components[$manifest->name] = $path;
40 1
            if ($manifest->watches !== null) {
41 1
                $this->add_watches($manifest->name, $manifest->watches);
42
            }
43
44 1
            $this->process_manifest($manifest, $container);
45
        }
46
47 1
        $cl = $container->getDefinition('componentloader');
48 1
        $cl->addArgument($components);
49
50 1
        $watcher = $container->getDefinition('watcher');
51 1
        $watcher->addArgument($this->watches);
52
53 1
        $dbclassloader = $container->getDefinition('dbclassloader');
54 1
        $dbclassloader->addArgument($this->classmap);
55
    }
56
57 1
    private function find_builtin_components() : array
58
    {
59 1
        $components = [];
60 1
        $finder = (new Finder())
61 1
            ->files()
62 1
            ->in([MIDCOM_ROOT, dirname(MIDCOM_ROOT) . '/src'])
63 1
            ->name('manifest.inc');
64 1
        foreach ($finder as $file) {
65 1
            $components[] = $file->getPathname();
66
        }
67 1
        return $components;
68
    }
69
70 1
    private function add_watches(string $component, array $watches)
71
    {
72 1
        foreach ($watches as $watch) {
73 1
            foreach (array_keys($this->watches) as $operation_id) {
74
                // Check whether the operations flag list from the component
75
                // contains the operation_id we're checking a watch for.
76 1
                if ($watch['operations'] & $operation_id) {
77 1
                    $this->watches[$operation_id][] = [
78 1
                        $component => $watch['classes']
79
                    ];
80
                }
81
            }
82
        }
83
    }
84
85
    /**
86
     * Register manifest data.
87
     *
88
     * All default privileges are made known to ACL, the watches are registered
89
     */
90 1
    private function process_manifest(midcom_core_manifest $manifest, ContainerBuilder $container)
91
    {
92
        // Register Privileges
93 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...
94 1
            $acl = $container->getDefinition('auth.acl');
95 1
            $acl->addMethodCall('register_default_privileges', [$manifest->privileges]);
96
        }
97 1
        $this->classmap[$manifest->name] = $manifest->class_mapping;
98 1
        if ($manifest->name == 'midcom') {
99 1
            $this->classmap['midcom'][$container->getParameter('midcom.person_class')] = \midcom_db_person::class;
100
        }
101
    }
102
}