Completed
Push — master ( bb6905...021686 )
by Grégoire
18s queued 12s
created

src/Controller/MediaAdminController.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createAction(Request $request = null)
31
    {
32
        $this->admin->checkAccess('create');
33
34
        if (!$request->get('provider') && $request->isMethod('get')) {
35
            $pool = $this->get('sonata.media.pool');
36
37
            return $this->render('@SonataMedia/MediaAdmin/select_provider.html.twig', [
38
                'providers' => $pool->getProvidersByContext(
39
                    $request->get('context', $pool->getDefaultContext())
0 ignored issues
show
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
40
                ),
41
                'action' => 'create',
42
            ]);
43
        }
44
45
        return parent::createAction();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function render($view, array $parameters = [], Response $response = null)
52
    {
53
        $parameters['media_pool'] = $this->get('sonata.media.pool');
54
        $parameters['persistent_parameters'] = $this->admin->getPersistentParameters();
55
56
        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...
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function listAction(Request $request = null)
63
    {
64
        $this->admin->checkAccess('list');
65
66
        if ($listMode = $request->get('_list_mode', 'mosaic')) {
0 ignored issues
show
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
67
            $this->admin->setListMode($listMode);
68
        }
69
70
        $datagrid = $this->admin->getDatagrid();
71
72
        $filters = $request->get('filter');
73
74
        // set the default context
75
        if (!$filters || !\array_key_exists('context', $filters)) {
76
            $context = $this->admin->getPersistentParameter('context', $this->get('sonata.media.pool')->getDefaultContext());
0 ignored issues
show
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...
77
        } else {
78
            $context = $filters['context']['value'];
79
        }
80
81
        $datagrid->setValue('context', null, $context);
82
83
        $rootCategory = null;
84
        if ($this->has('sonata.media.manager.category')) {
85
            $rootCategory = $this->get('sonata.media.manager.category')->getRootCategory($context);
86
        }
87
88
        if (null !== $rootCategory && !$filters) {
89
            $datagrid->setValue('category', null, $rootCategory->getId());
90
        }
91
        if ($this->has('sonata.media.manager.category') && $request->get('category')) {
92
            $category = $this->get('sonata.media.manager.category')->findOneBy([
93
                'id' => (int) $request->get('category'),
94
                'context' => $context,
95
            ]);
96
97
            if (!empty($category)) {
98
                $datagrid->setValue('category', null, $category->getId());
99
            } else {
100
                $datagrid->setValue('category', null, $rootCategory->getId());
101
            }
102
        }
103
104
        $formView = $datagrid->getForm()->createView();
105
106
        $this->setFormTheme($formView, $this->admin->getFilterTheme());
107
108
        return $this->render($this->admin->getTemplate('list'), [
109
            'action' => 'list',
110
            'form' => $formView,
111
            'datagrid' => $datagrid,
112
            'root_category' => $rootCategory,
113
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
114
        ]);
115
    }
116
117
    /**
118
     * Sets the admin form theme to form view. Used for compatibility between Symfony versions.
119
     */
120
    private function setFormTheme(FormView $formView, array $theme)
0 ignored issues
show
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...
121
    {
122
        $twig = $this->get('twig');
123
124
        $twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
125
    }
126
}
127