Completed
Push — master ( e25986...ffddd1 )
by Craig
07:06
created

EditorController::indexAction()   C

Complexity

Conditions 9
Paths 107

Size

Total Lines 62
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 46
nc 107
nop 1
dl 0
loc 62
rs 6.4959
c 0
b 0
f 0

How to fix   Long Method   

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\Controller;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Zikula\ThemeModule\Engine\Annotation\Theme;
21
22
/**
23
 * Workflow editor controller class.
24
 *
25
 * @Route("/editor")
26
 */
27
class EditorController extends Controller
28
{
29
    /**
30
     * This is the default action handling the index action.
31
     *
32
     * @Route("/index",
33
     *        methods = {"GET"}
34
     * )
35
     * @Theme("admin")
36
     * @Template
37
     *
38
     * @param Request $request Current request instance
39
     *
40
     * @return Response Output
41
     *
42
     * @throws AccessDeniedException Thrown if the user doesn't have required permissions
43
     * @throws NotFoundHttpException Thrown if the desired workflow could not be found
44
     */
45
    public function indexAction(Request $request)
46
    {
47
        if (!$this->get('zikula_permissions_module.api.permission')->hasPermission('ZikulaWorkflowBundle::', '::', ACCESS_ADMIN)) {
48
            throw new AccessDeniedException();
49
        }
50
51
        $workflowType = 'workflow';
52
        $workflowName = $workflowType . '.' . $request->query->get('workflow', '');
53
        if (!$this->get('service_container')->has($workflowName)) {
54
            $workflowType = 'state_machine';
55
            $workflowName = $workflowType . '.' . $request->query->get('workflow', '');
56
        }
57
        if (!$this->get('service_container')->has($workflowName)) {
58
            throw new NotFoundHttpException($this->get('translator.default')->__f('Workflow "%workflow%" not found.', ['%workflow%' => $workflowName]));
59
        }
60
61
        $workflow = $this->get($workflowName);
62
        $workflowDefinition = $workflow->getDefinition();
63
64
        $markingStoreType = '';
65
        $markingStoreField = '';
66
        $supportedEntityClassNames = [];
67
        try {
68
            $reflection = new \ReflectionClass('Symfony\Component\Workflow\Workflow');
69
            $markingStoreProperty = $reflection->getProperty('markingStore');
70
            $markingStoreProperty->setAccessible(true);
71
            $markingStore = $markingStoreProperty->getValue($workflow);
72
            if ($markingStore instanceof \Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Workfl...ltipleStateMarkingStore does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
                $markingStoreType = 'multiple_state';
74
            } elseif ($markingStore instanceof \Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Workfl...SingleStateMarkingStore does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
75
                $markingStoreType = 'single_state';
76
            }
77
78
            $reflection = new \ReflectionClass(get_class($markingStore));
79
            $markingStoreFieldProperty = $reflection->getProperty('property');
80
            $markingStoreFieldProperty->setAccessible(true);
81
            $markingStoreField = $markingStoreFieldProperty->getValue($markingStore);
82
83
            $registry = $this->get('workflow.registry');
84
            $reflection = new \ReflectionClass(get_class($registry));
85
            $workflowsProperty = $reflection->getProperty('workflows');
86
            $workflowsProperty->setAccessible(true);
87
            $workflows = $workflowsProperty->getValue($registry);
88
            foreach ($workflows as list($aWorkflow, $className)) {
89
                if ($aWorkflow->getName() == $workflow->getName()) {
90
                    $supportedEntityClassNames[] = $className;
91
                }
92
            }
93
        } catch (\ReflectionException $e) {
94
            $markingStoreType = 'single_state';
95
            $markingStoreField = 'state';
96
        }
97
98
        return [
99
            'name' => $workflow->getName(),
100
            'type' => $workflowType,
101
            'markingStoreType' => $markingStoreType,
102
            'markingStoreField' => $markingStoreField,
103
            'supportedEntities' => $supportedEntityClassNames,
104
            'workflow' => $workflowDefinition
105
        ];
106
    }
107
}
108