Passed
Pull Request — master (#334)
by
unknown
06:45
created

MediaRepository::findByPhrase()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 40
rs 8.9617
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Repository;
14
15
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
16
use Doctrine\ORM\QueryBuilder;
17
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
18
19
class MediaRepository extends EntityRepository implements MediaRepositoryInterface
20
{
21
    public function createListQueryBuilder(): QueryBuilder
22
    {
23
        return $this->createQueryBuilder('o')
24
            ->leftJoin('o.translations', 'translation')
25
        ;
26
    }
27
28
    public function findOneEnabledByCode(string $code, string $channelCode): ?MediaInterface
29
    {
30
        return $this->createQueryBuilder('o')
31
            ->innerJoin('o.channels', 'channel')
32
            ->where('o.code = :code')
33
            ->andWhere('o.enabled = true')
34
            ->setParameter('code', $code)
35
            ->getQuery()
36
            ->getOneOrNullResult()
37
        ;
38
    }
39
40
    public function findBySectionCode(string $sectionCode, string $channelCode): array
41
    {
42
        return $this->createQueryBuilder('o')
43
            ->innerJoin('o.channels', 'channel')
44
            ->innerJoin('o.sections', 'section')
45
            ->where('channel.code = :channelCode')
46
            ->andWhere('section.code = :sectionCode')
47
            ->andWhere('o.enabled = true')
48
            ->setParameter('channelCode', $channelCode)
49
            ->setParameter('sectionCode', $sectionCode)
50
            ->getQuery()
51
            ->getResult()
52
        ;
53
    }
54
55
    public function findByProductCode(string $productCode, string $channelCode): array
56
    {
57
        return $this->createQueryBuilder('o')
58
            ->innerJoin('o.channels', 'channel')
59
            ->innerJoin('o.products', 'product')
60
            ->where('channel.code = :channelCode')
61
            ->andWhere('product.code = :productCode')
62
            ->andWhere('o.enabled = true')
63
            ->setParameter('channelCode', $channelCode)
64
            ->setParameter('productCode', $productCode)
65
            ->getQuery()
66
            ->getResult()
67
        ;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function findByPhrase(
74
        ?string $phrase,
75
        ?string $mediaType = null,
76
        int $limit = self::DEFAULT_LIMIT,
77
        int $page = self::DEFAULT_PAGE
78
    ): array {
79
        $qb = $this->createListQueryBuilder();
80
        $expr = $qb->expr();
81
82
        $qb->andWhere('o.enabled = true');
83
84
        if (null !== $phrase && strlen($phrase) > 0) {
85
            $qb
86
                ->andWhere($expr->orX(
87
                    $expr->like('o.code', ':phrase'),
88
                    $expr->like('translation.name', ':phrase')
89
                ))
90
                ->setParameter(
91
                    'phrase',
92
                    '%' . addcslashes((string)$phrase, "%_") . '%'
93
                )
94
            ;
95
        }
96
97
        if (null !== $mediaType) {
98
            $qb
99
                ->andWhere('o.type = :mediaType')
100
                ->setParameter('mediaType', $mediaType)
101
            ;
102
        }
103
        
104
        $qb->orderBy('o.code');
105
106
        $paginator = $this->getPaginator($qb);
107
        $paginator
108
            ->setMaxPerPage($limit > 0 ? $limit : self::DEFAULT_LIMIT)
109
            ->setCurrentPage($page > 0 ? $page : self::DEFAULT_PAGE)
110
        ;
111
112
        return (array) $paginator->getCurrentPageResults();
113
    }
114
}
115