Completed
Push — master ( 579211...343140 )
by Craig
06:19
created

ZikulaWorkflowExtension::loadWorkflowDefinitions()   D

Complexity

Conditions 10
Paths 35

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 35
nop 1
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\WorkflowBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * Class ZikulaWorkflowExtension
24
 */
25
class ZikulaWorkflowExtension extends Extension implements PrependExtensionInterface
26
{
27
    private $workflowDirectories = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        // unrequired
35
        //$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
        //$loader->load('services.yml');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function prepend(ContainerBuilder $container)
43
    {
44
        // central workflows in the core system are placed in: lib/Zikula/Bundle/CoreBundle/Resources/workflows/
45
        $this->workflowDirectories[] = __DIR__ . '/../Resources/workflows';
46
47
        $rootDirectory = $container->getParameter('kernel.root_dir');
48
49
        // Modules can define their own workflows in: modules/Acme/MyBundle/Resources/workflows/
50
        $this->workflowDirectories[] = $rootDirectory . '/../system/*/Resources/workflows';
51
        $this->workflowDirectories[] = $rootDirectory . '/../modules/*/*/Resources/workflows';
52
53
        // also it is possible to define custom workflows (or override existing ones) in: app/Resources/workflows/
54
        $this->workflowDirectories[] = $rootDirectory . '/Resources/workflows';
55
56
        $this->loadWorkflowDefinitions($container);
57
    }
58
59
    /**
60
     * Loads workflow files from given directories.
61
     *
62
     * @param ContainerBuilder $container
63
     *
64
     * @todo replace Finder usage by glob pattern in master branch
65
     * @see http://symfony.com/blog/new-in-symfony-3-3-import-config-files-with-glob-patterns
66
     */
67
    private function loadWorkflowDefinitions(ContainerBuilder $container)
68
    {
69
        // get all bundles
70
        $bundleNames = array_keys($container->getParameter('kernel.bundles'));
71
72
        try {
73
            $finder = new Finder();
74
            $finder->files()->name('*.yml')->in($this->workflowDirectories);
75
            foreach ($finder as $file) {
76
                $filePath = $file->getPath();
77
                if (false !== strpos($filePath, 'modules/')) {
78
                    // check if the module is installed and active
79
                    $composerFile = str_replace('/Resources/workflows', '', $filePath) . '/composer.json';
80
                    if (!file_exists($composerFile)) {
81
                        // no composer file, skip the module
82
                        continue;
83
                    }
84
                    $composerData = json_decode(file_get_contents($composerFile));
85
                    if (!isset($composerData->extra) || !isset($composerData->extra->zikula) || !isset($composerData->extra->zikula->{'class'})) {
86
                        // no zikula extra information, skip the module
87
                        continue;
88
                    }
89
90
                    $moduleClass = $composerData->extra->zikula->{'class'};
91
                    $moduleClassParts = explode('\\', $moduleClass);
92
                    $moduleName = array_pop($moduleClassParts);
93
                    if (!in_array($moduleName, $bundleNames)) {
94
                        // module is not active, skip it
95
                        continue;
96
                    }
97
                }
98
                $loader = new YamlFileLoader($container, new FileLocator($filePath));
99
                $loader->load($file->getFilename());
100
            }
101
102
            $finder = new Finder();
103
            $finder->files()->name('*.xml')->in($this->workflowDirectories);
104
            foreach ($finder as $file) {
105
                $loader = new XmlFileLoader($container, new FileLocator($file->getPath()));
106
                $loader->load($file->getFilename());
107
            }
108
        } catch (\InvalidArgumentException $e) {
109
            // no module with a workflow directory exists, ignore
110
        }
111
    }
112
}
113