MediaManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 23 5
A getPager() 0 34 5
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\MediaManagerInterface;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class MediaManager extends BaseEntityManager implements MediaManagerInterface
25
{
26
    public function save($media, $andFlush = true): void
27
    {
28
        /*
29
         * Warning: previous method signature was : save(MediaInterface $media, $context = null, $providerName = null)
30
         */
31
32
        // BC compatibility for $context parameter
33
        if ($andFlush && \is_string($andFlush)) {
34
            $media->setContext($andFlush);
35
        }
36
37
        // BC compatibility for $providerName parameter
38
        if (3 === \func_num_args()) {
39
            $media->setProviderName(func_get_arg(2));
40
        }
41
42
        if (\is_bool($andFlush)) {
43
            parent::save($media, $andFlush);
44
        } else {
45
            // BC compatibility with previous signature
46
            parent::save($media, true);
47
        }
48
    }
49
50
    public function getPager(array $criteria, $page, $limit = 10, array $sort = [])
51
    {
52
        $query = $this->getRepository()
53
            ->createQueryBuilder('m')
54
            ->select('m');
55
56
        $fields = $this->getEntityManager()->getClassMetadata($this->class)->getFieldNames();
57
        foreach ($sort as $field => $direction) {
58
            if (!\in_array($field, $fields, true)) {
59
                throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
60
            }
61
        }
62
63
        foreach ($sort as $field => $direction) {
64
            $query->orderBy(sprintf('m.%s', $field), strtoupper($direction));
65
        }
66
67
        $parameters = [];
68
69
        if (isset($criteria['enabled'])) {
70
            $query->andWhere('m.enabled = :enabled');
71
            $parameters['enabled'] = $criteria['enabled'];
72
        }
73
74
        $query->setParameters($parameters);
75
76
        $pager = new Pager();
77
        $pager->setMaxPerPage($limit);
78
        $pager->setQuery(new ProxyQuery($query));
79
        $pager->setPage($page);
80
        $pager->init();
81
82
        return $pager;
83
    }
84
}
85