ControllerWorkflowFeaturesTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 93
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
get() 0 1 ?
createForm() 0 1 ?
A workflowStatutAvailable() 0 14 2
A changeWorkflowStatut() 0 21 2
A checkIfElementCanChangeStatut() 0 12 3
1
<?php
2
3
namespace QualityCode\ApiFeaturesBundle\Controller;
4
5
use FOS\RestBundle\View\View;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Workflow\Workflow;
9
10
/**
11
 * Add workflow features on a controller.
12
 *
13
 * @author flavien-metivier
14
 */
15
trait ControllerWorkflowFeaturesTrait
16
{
17
    /**
18
     * Gets a container service by its id.
19
     *
20
     * @param string $id The service id
21
     *
22
     * @return object The service
23
     */
24
    abstract protected function get($id);
25
26
    /**
27
     * Creates and returns a Form instance from the type of the form.
28
     *
29
     * @param string $type    The fully qualified class name of the form type
30
     * @param mixed  $data    The initial data for the form
31
     * @param array  $options Options for the form
32
     *
33
     * @return Form
34
     */
35
    abstract protected function createForm($type, $data = null, array $options = []);
36
37
    /**
38
     * @param Request $request
39
     * @param string  $repositoryName
40
     * @param string  $workflowName
41
     *
42
     * @return mixed
43
     */
44
    protected function workflowStatutAvailable(Request $request, $repositoryName, $workflowName)
45
    {
46
        $element = $this->get('doctrine.orm.entity_manager')
47
                ->getRepository($repositoryName)
48
                ->find($request->get('id'));
49
50
        if (empty($element)) {
51
            return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND);
52
        }
53
54
        $workflow = $this->get('workflow.'.$workflowName);
55
56
        return $workflow->getEnabledTransitions($element);
57
    }
58
59
    /**
60
     * @param Request $request
61
     * @param string  $repositoryName
62
     * @param string  $workflowName
63
     *
64
     * @return mixed
65
     */
66
    protected function changeWorkflowStatut(Request $request, $repositoryName, $workflowName)
67
    {
68
        $element = $this->get('doctrine.orm.entity_manager')
69
                ->getRepository($repositoryName)
70
                ->find($request->get('id'));
71
72
        $newState = $request->get('state_name');
73
        $workflow = $this->get('workflow.'.$workflowName);
74
75
        $can = $this->checkIfElementCanChangeStatut($newState, $workflow, $element);
76
        if ($can !== false) {
77
            return $can;
78
        }
79
80
        $workflow->apply($element, $newState);
81
        $em = $this->get('doctrine.orm.entity_manager');
82
        $em->persist($element);
83
        $em->flush();
84
85
        return $element;
86
    }
87
88
    /**
89
     * @param string   $newState
90
     * @param Workflow $workflow
91
     * @param mixed    $element
92
     *
93
     * @return bool|\FOS\RestBundle\View\View
94
     */
95
    protected function checkIfElementCanChangeStatut(string $newState, Workflow $workflow, $element)
96
    {
97
        if (empty($element)) {
98
            return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND);
99
        }
100
101
        if (!$workflow->can($element, $newState)) {
102
            return View::create(['message' => 'Workflow state not available'], Response::HTTP_FORBIDDEN);
103
        }
104
105
        return true;
106
    }
107
}
108