MediaAdminController::listAction()   B
last analyzed

Complexity

Conditions 10
Paths 48

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.1369
c 0
b 0
f 0
cc 10
nc 48
nop 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Controller;
15
16
use Sonata\AdminBundle\Controller\CRUDController as Controller;
17
use Symfony\Component\Form\FormRenderer;
18
use Symfony\Component\Form\FormView;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * @final since sonata-project/media-bundle 3.21.0
24
 */
25
class MediaAdminController extends Controller
26
{
27
    public function createAction(?Request $request = null)
28
    {
29
        $this->admin->checkAccess('create');
30
31
        if (!$request->get('provider') && $request->isMethod('get')) {
32
            $pool = $this->get('sonata.media.pool');
33
34
            return $this->render('@SonataMedia/MediaAdmin/select_provider.html.twig', [
35
                'providers' => $pool->getProvidersByContext(
36
                    $request->get('context', $pool->getDefaultContext())
37
                ),
38
                'action' => 'create',
39
            ]);
40
        }
41
42
        return parent::createAction();
43
    }
44
45
    public function render($view, array $parameters = [], ?Response $response = null)
46
    {
47
        $parameters['media_pool'] = $this->get('sonata.media.pool');
48
        $parameters['persistent_parameters'] = $this->admin->getPersistentParameters();
49
50
        return parent::renderWithExtraParams($view, $parameters, $response);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (renderWithExtraParams() instead of render()). Are you sure this is correct? If so, you might want to change this to $this->renderWithExtraParams().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
51
    }
52
53
    public function listAction(?Request $request = null)
54
    {
55
        $this->admin->checkAccess('list');
56
57
        if ($listMode = $request->get('_list_mode', 'mosaic')) {
58
            $this->admin->setListMode($listMode);
59
        }
60
61
        $datagrid = $this->admin->getDatagrid();
62
63
        $filters = $request->get('filter');
64
65
        // set the default context
66
        if (!$filters || !\array_key_exists('context', $filters)) {
67
            $context = $this->admin->getPersistentParameter('context', $this->get('sonata.media.pool')->getDefaultContext());
0 ignored issues
show
Bug introduced by
The method getPersistentParameter() does not exist on Sonata\AdminBundle\Admin\AdminInterface. Did you maybe mean getPersistentParameters()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
        } else {
69
            $context = $filters['context']['value'];
70
        }
71
72
        $datagrid->setValue('context', null, $context);
73
74
        $rootCategory = null;
75
        if ($this->has('sonata.media.manager.category')) {
76
            $rootCategory = $this->get('sonata.media.manager.category')->getRootCategory($context);
77
        }
78
79
        if (null !== $rootCategory && !$filters) {
80
            $datagrid->setValue('category', null, $rootCategory->getId());
81
        }
82
        if ($this->has('sonata.media.manager.category') && $request->get('category')) {
83
            $category = $this->get('sonata.media.manager.category')->findOneBy([
84
                'id' => (int) $request->get('category'),
85
                'context' => $context,
86
            ]);
87
88
            if (!empty($category)) {
89
                $datagrid->setValue('category', null, $category->getId());
90
            } else {
91
                $datagrid->setValue('category', null, $rootCategory->getId());
92
            }
93
        }
94
95
        $formView = $datagrid->getForm()->createView();
96
97
        $this->setFormTheme($formView, $this->admin->getFilterTheme());
98
99
        return $this->render($this->admin->getTemplate('list'), [
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...nterface::getTemplate() has been deprecated with message: since sonata-project/admin-bundle 3.35. To be removed in 4.0. Use TemplateRegistry services instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
            'action' => 'list',
101
            'form' => $formView,
102
            'datagrid' => $datagrid,
103
            'root_category' => $rootCategory,
104
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
105
        ]);
106
    }
107
108
    /**
109
     * Sets the admin form theme to form view. Used for compatibility between Symfony versions.
110
     */
111
    private function setFormTheme(FormView $formView, array $theme)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
112
    {
113
        $twig = $this->get('twig');
114
115
        $twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
116
    }
117
}
118