Passed
Pull Request — master (#123)
by Arnaud
03:13
created

ViewHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isFormValid() 0 20 5
A shouldRedirect() 0 31 6
1
<?php
2
3
namespace LAG\AdminBundle\View;
4
5
use LAG\AdminBundle\Admin\ActionInterface;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use LAG\AdminBundle\Configuration\AdminConfiguration;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class ViewHelper
11
{
12
    public function shouldRedirect(
13
        Request $request,
14
        AdminConfiguration $configuration,
15
        AdminInterface $admin,
16
        ActionInterface $action,
17
        string $formName = ''
18
    ) {
19
        if (!$this->isFormValid($admin, $configuration, $formName)) {
20
            return false;
21
        }
22
23
        // When the create form is submitted, the user should be redirected to the edit action after saving the form
24
        // data
25
        if ('create' === $action->getName()) {
26
            return true;
27
        }
28
29
        // When the delete form is submitted, we should redirect to the list action
30
        if ('delete' === $action->getName()) {
31
            return true;
32
        }
33
34
        if (!$request->get('submit_and_redirect')) {
35
            return false;
36
        }
37
38
        if ('submit_and_redirect' !== $request->get('submit_and_redirect')) {
39
            return false;
40
        }
41
42
        return true;
43
    }
44
45
    private function isFormValid(AdminInterface $admin, AdminConfiguration $configuration, string $formName): bool
46
    {
47
        if (!$admin->hasForm($formName)) {
48
            return false;
49
        }
50
        $form = $admin->getForm($formName);
51
52
        if (!$form->isSubmitted()) {
53
            return false;
54
        }
55
56
        if (!$form->isValid()) {
57
            return false;
58
        }
59
60
        if (!key_exists('list', $configuration->getParameter('actions'))) {
61
            return false;
62
        }
63
64
        return true;
65
    }
66
}
67