GridViewSubscriber::onView()   D
last analyzed

Complexity

Conditions 10
Paths 23

Size

Total Lines 36
Code Lines 21

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
dl 3
loc 36
rs 4.8196
c 0
b 0
f 0
ccs 24
cts 24
cp 1
cc 10
eloc 21
nc 23
nop 1
crap 10

How to fix   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
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber;
13
14
use Lug\Bundle\GridBundle\Form\Type\Batch\GridBatchType;
15
use Lug\Bundle\GridBundle\View\GridViewInterface;
16
use Lug\Bundle\ResourceBundle\Form\FormFactoryInterface;
17
use Lug\Bundle\ResourceBundle\Rest\AbstractSubscriber;
18
use Lug\Bundle\ResourceBundle\Rest\RestEvents;
19
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent;
20
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\Form\FormRendererInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class GridViewSubscriber extends AbstractSubscriber
28
{
29
    /**
30
     * @var FormFactoryInterface
31
     */
32
    private $formFactory;
33
34
    /**
35
     * @var FormRendererInterface
36
     */
37
    private $formRenderer;
38
39
    /**
40
     * @param ParameterResolverInterface $parameterResolver
41
     * @param FormFactoryInterface       $formFactory
42
     * @param FormRendererInterface      $formRenderer
43
     */
44 11
    public function __construct(
45
        ParameterResolverInterface $parameterResolver,
46
        FormFactoryInterface $formFactory,
47
        FormRendererInterface $formRenderer
48
    ) {
49 11
        parent::__construct($parameterResolver);
50
51 11
        $this->formFactory = $formFactory;
52 11
        $this->formRenderer = $formRenderer;
53 11
    }
54
55
    /**
56
     * @param ViewEvent $event
57
     */
58 3
    public function onApi(ViewEvent $event)
59
    {
60 3
        if (!$this->getParameterResolver()->resolveApi()) {
61 1
            return;
62
        }
63
64 2
        $view = $event->getView();
65 2
        $data = $view->getData();
66
67 2 View Code Duplication
        if (is_array($data) && isset($data['grid']) && $data['grid'] instanceof GridViewInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 1
            $data = $data['grid'];
69 1
        }
70
71 2
        if ($data instanceof GridViewInterface) {
72 1
            $view->setData($data->getDataSource());
73 1
        }
74 2
    }
75
76
    /**
77
     * @param ViewEvent $event
78
     */
79 5
    public function onView(ViewEvent $event)
80
    {
81 5
        if ($this->getParameterResolver()->resolveApi()) {
82 1
            return;
83
        }
84
85 4
        $view = $event->getView();
86 4
        $data = $grid = $view->getData();
87
88 4 View Code Duplication
        if (is_array($data) && isset($data['grid']) && $data['grid'] instanceof GridViewInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89 3
            $grid = $data['grid'];
90 3
        }
91
92 4
        if (!$grid instanceof GridViewInterface) {
93 1
            return;
94
        }
95
96 3
        if ($grid->getBatchForm() === null) {
97 3
            $batchForm = !isset($data['batch_form']) || !$data['batch_form'] instanceof FormInterface
98 3
                ? $this->formFactory->create(GridBatchType::class, null, ['grid' => $grid])
99 3
                : $data['batch_form'];
100
101 3
            $grid->setBatchForm($batchForm->createView());
102 3
        }
103
104 3
        $themes = $this->getParameterResolver()->resolveThemes();
105
106 3
        if (!empty($themes)) {
107 1
            $this->formRenderer->setTheme($grid->getForm(), $themes);
0 ignored issues
show
Bug introduced by
It seems like $grid->getForm() can be null; however, setTheme() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
108 1
            $this->formRenderer->setTheme($grid->getBatchForm(), $themes);
0 ignored issues
show
Bug introduced by
It seems like $grid->getBatchForm() can be null; however, setTheme() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
109 1
        }
110
111
        $view
112 3
            ->setTemplateVar('grid')
113 3
            ->setData($grid);
114 3
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121 1
        return [RestEvents::VIEW => [
122 1
            ['onApi', -2000],
123 1
            ['onView', -1000],
124 1
        ]];
125
    }
126
}
127