Passed
Pull Request — master (#334)
by
unknown
08:46
created

MediaRepository::findByPhrase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 22
rs 9.8333
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
     * @return MediaInterface[]
72
     */
73
    public function findByPhrase(string $phrase, ?string $mediaType = null): array
74
    {
75
        $qb = $this->createListQueryBuilder();
76
        $expr = $qb->expr();
77
78
        $qb
79
            ->andWhere($expr->orX(
80
                $expr->like('o.code', ':phrase'),
81
                $expr->like('translation.name', ':phrase')
82
            ))
83
            ->andWhere('o.enabled = true')
84
            ->setParameter('phrase', '%'.addcslashes($phrase, "%_").'%')
85
        ;
86
87
        if (null !== $mediaType) {
88
            $qb
89
                ->andWhere('o.type = :mediaType')
90
                ->setParameter('mediaType', $mediaType)
91
            ;
92
        }
93
94
        return $qb->getQuery()->getResult();
95
    }
96
}
97