Completed
Push — master ( 73da01...28cf8c )
by Wesley
04:37 queued 01:27
created

WorkflowActionGenerator::getActionsForSubject()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
nc 5
nop 1
dl 0
loc 56
rs 9.0648
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
declare(strict_types=1);
4
5
/*
6
 * (c) 2019, Wesley O. Nichols
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 Wesnick\WorkflowBundle;
13
14
use ApiPlatform\Core\Api\IriConverterInterface;
15
use ApiPlatform\Core\Api\UrlGeneratorInterface;
16
use Symfony\Component\Validator\ConstraintViolation;
17
use Symfony\Component\Workflow\Registry;
18
use Symfony\Component\Workflow\Transition;
19
use Wesnick\WorkflowBundle\Model\Action;
20
use Wesnick\WorkflowBundle\Model\EntryPoint;
21
22
/**
23
 * Class WorkflowActionGenerator.
24
 *
25
 * @author Wesley O. Nichols <[email protected]>
26
 */
27
class WorkflowActionGenerator
28
{
29
    private $registry;
30
    private $iriConverter;
31
32
    /**
33
     * Classmap.
34
     *
35
     * [ className => [workflowName]]
36
     *
37
     * @var array
38
     */
39
    private $enabledWorkflowMap;
40
41
    /**
42
     * WorkflowActionGenerator constructor.
43
     *
44
     * @param $registry
45
     * @param $iriConverter
46
     * @param array $enabledWorkflowMap
47
     */
48
    public function __construct(Registry $registry, IriConverterInterface $iriConverter, array $enabledWorkflowMap)
49
    {
50
        $this->registry = $registry;
51
        $this->iriConverter = $iriConverter;
52
        $this->enabledWorkflowMap = $enabledWorkflowMap;
53
    }
54
55
    public function getActionsForSubject($subject)
56
    {
57
        $workflows = $this->registry->all($subject);
58
        $actions = [];
59
60
        foreach ($workflows as $workflow) {
61
            /** @var Transition $transition */
62
            foreach ($workflow->getEnabledTransitions($subject) as $transition) {
63
                $transitionMeta = $workflow->getMetadataStore()->getTransitionMetadata($transition);
64
65
                $blockers = $workflow->buildTransitionBlockerList($subject, $transition->getName());
66
67
                $url = sprintf(
68
                    '%s?%s',
69
                    $this->iriConverter->getIriFromItem($subject, UrlGeneratorInterface::ABS_URL),
70
                    http_build_query([
71
                        'workflow' => $workflow->getName(),
72
                        'transition' => $transition->getName(),
73
                    ])
74
                );
75
76
                $entryPoint = new EntryPoint();
77
                $entryPoint->setUrl($url);
78
                $entryPoint->setHttpMethod('PATCH');
79
80
                $currentAction = new Action();
81
                $currentAction->setTarget($entryPoint);
82
                $currentAction->setName($transition->getName());
83
                $currentAction->setDescription($transitionMeta['description'] ?? ucfirst($transition->getName()).' Action');
84
                // @TODO: add sub status (available, unavailable, access denied, invalid)
85
86
                foreach ($blockers as $blocker) {
87
                    $parameters = $blocker->getParameters();
88
89
                    if (array_key_exists('original_violation', $parameters)) {
90
                        $violation = $parameters['original_violation'];
91
                    } else {
92
                        // @TODO: add a factory or event for building Violations from TransitionBlockers
93
                        $violation = new ConstraintViolation(
94
                            $blocker->getMessage(),
95
                            $blocker->getMessage(),
96
                            $blocker->getParameters(),
97
                            $subject,
98
                            '/',
99
                            ''
100
                        );
101
                    }
102
103
                    $currentAction->addError($violation);
104
                }
105
106
                $actions[] = $currentAction;
107
            }
108
        }
109
110
        return $actions;
111
    }
112
}
113