Passed
Pull Request — master (#357)
by
unknown
12:23
created

ViewerController::showEntityHistoryAction()   B

Complexity

Conditions 11
Paths 59

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 35
c 2
b 0
f 0
nc 59
nop 4
dl 0
loc 52
rs 7.3166

How to fix   Long Method    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
namespace DH\AuditorBundle\Controller;
4
5
use DH\Auditor\Exception\AccessDeniedException;
6
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\DateRangeFilter;
7
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\SimpleFilter;
8
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Query;
9
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
10
use DH\AuditorBundle\Form\FilterForm;
11
use DH\AuditorBundle\Helper\UrlHelper;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * Overrides DH\AuditorBundle\Controller\ViewerController
19
 */
20
class ViewerController extends AbstractController
21
{
22
    /**
23
     * @Route(path="/audit/{entity}/{id}", name="dh_auditor_show_entity_history_2", methods={"GET", "POST"})
24
     *
25
     * @param int|string $id
26
     */
27
    public function showEntityHistoryAction(Request $request, Reader $reader, string $entity, $id = null): Response
28
    {
29
        $entity = UrlHelper::paramToNamespace($entity);
30
31
        if (!$reader->getProvider()->isAuditable($entity)) {
32
            throw $this->createNotFoundException();
33
        }
34
35
        $supportedFilters = Query::getSupportedFilters();
0 ignored issues
show
Bug Best Practice introduced by
The method DH\Auditor\Provider\Doct...::getSupportedFilters() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $supportedFilters = Query::getSupportedFilters();
Loading history...
36
        $form = $this->createForm(FilterForm::class);
37
38
        $form->handleRequest($request);
39
40
        try {
41
            $page = (int) $form->get('page')->getData();
42
            $page = $page < 1 ? 1 : $page;
43
            $query = $reader->createQuery($entity, [
44
                'object_id' => $id,
45
                'page' => $page,
46
                'page_size' => Reader::PAGE_SIZE,
47
            ]);
48
49
            if ($form->isSubmitted() && $form->isValid()) {
50
                $min = $form->get('created_at_start')->getData();
51
                $max = $form->get('created_at_end')->getData();
52
53
                if ($min || $max) {
54
                    $query->addFilter(new DateRangeFilter('created_at', $min, $max));
55
                }
56
                foreach ($form->all() as $field) {
57
                    $data = $field->getData();
58
                    if (!$data || \in_array($field->getName(), ['page', 'created_at_start', 'created_at_end'])) {
59
                        continue;
60
                    }
61
                    dump($field->getName());
62
                    dump($data);
63
                    $query->addFilter(new SimpleFilter($field->getName(), $data));
64
                }
65
            }
66
67
            $pager = $reader->paginate($query, $page, Reader::PAGE_SIZE);
68
        } catch (AccessDeniedException $e) {
69
            throw $this->createAccessDeniedException();
70
        }
71
72
        return $this->render('@DHAuditor/Audit/entity_history.html.twig', [
73
            'id' => $id,
74
            'entity' => $entity,
75
            'paginator' => $pager,
76
            'supportedFilters' => $supportedFilters,
77
            'form' => $form->createView(),
78
            'page' => $form->get('page')->getData(),
79
        ]);
80
    }
81
}
82