GalleryManager::getPager()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
cc 6
nc 17
nop 4
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\Entity;
15
16
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
17
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
18
use Sonata\Doctrine\Entity\BaseEntityManager;
19
use Sonata\MediaBundle\Model\GalleryInterface;
20
use Sonata\MediaBundle\Model\GalleryManagerInterface;
21
22
/**
23
 * @final since sonata-project/media-bundle 3.21.0
24
 */
25
class GalleryManager extends BaseEntityManager implements GalleryManagerInterface
26
{
27
    /**
28
     * BC Compatibility.
29
     *
30
     * NEXT_MAJOR: remove this method.
31
     *
32
     * @deprecated Please use save() from now
33
     */
34
    public function update(GalleryInterface $gallery): void
35
    {
36
        parent::save($gallery);
0 ignored issues
show
Documentation introduced by
$gallery is of type object<Sonata\MediaBundle\Model\GalleryInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (save() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->save().

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...
37
    }
38
39
    public function getPager(array $criteria, $page, $limit = 10, array $sort = [])
40
    {
41
        $query = $this->getRepository()
42
            ->createQueryBuilder('g')
43
            ->select('g');
44
45
        $fields = $this->getEntityManager()->getClassMetadata($this->class)->getFieldNames();
46
        foreach ($sort as $field => $direction) {
47
            if (!\in_array($field, $fields, true)) {
48
                throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
49
            }
50
        }
51
        if (0 === \count($sort)) {
52
            $sort = ['name' => 'ASC'];
53
        }
54
        foreach ($sort as $field => $direction) {
55
            $query->orderBy(sprintf('g.%s', $field), strtoupper($direction));
56
        }
57
58
        $parameters = [];
59
60
        if (isset($criteria['enabled'])) {
61
            $query->andWhere('g.enabled = :enabled');
62
            $parameters['enabled'] = $criteria['enabled'];
63
        }
64
65
        $query->setParameters($parameters);
66
67
        $pager = new Pager();
68
        $pager->setMaxPerPage($limit);
69
        $pager->setQuery(new ProxyQuery($query));
70
        $pager->setPage($page);
71
        $pager->init();
72
73
        return $pager;
74
    }
75
}
76