MediaRepository::findByProductCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 3
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Repository;
12
13
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
16
17
class MediaRepository extends EntityRepository implements MediaRepositoryInterface
18
{
19
    public function createListQueryBuilder(string $locale): QueryBuilder
20
    {
21
        return $this->createQueryBuilder('o')
22
            ->leftJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
23
            ->setParameter('locale', $locale)
24
        ;
25
    }
26
27
    public function findOneEnabledByCode(
28
        string $code,
29
        string $localeCode,
30
        string $channelCode
31
    ): ?MediaInterface {
32
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return BitBag\SyliusCmsPlugin\Entity\MediaInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
33
            ->leftJoin('o.translations', 'translation')
34
            ->innerJoin('o.channels', 'channels')
35
            ->where('translation.locale = :localeCode')
36
            ->andWhere('o.code = :code')
37
            ->andWhere('o.enabled = true')
38
            ->andWhere('channels.code = :channelCode')
39
            ->setParameter('code', $code)
40
            ->setParameter('localeCode', $localeCode)
41
            ->setParameter('channelCode', $channelCode)
42
            ->getQuery()
43
            ->getOneOrNullResult()
44
        ;
45
    }
46
47
    public function findBySectionCode(
48
        string $sectionCode,
49
        string $localeCode,
50
        string $channelCode
51
    ): array {
52
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
53
            ->leftJoin('o.translations', 'translation')
54
            ->innerJoin('o.sections', 'section')
55
            ->innerJoin('o.channels', 'channels')
56
            ->andWhere('translation.locale = :localeCode')
57
            ->andWhere('section.code = :sectionCode')
58
            ->andWhere('o.enabled = true')
59
            ->andWhere('channels.code = :channelCode')
60
            ->setParameter('localeCode', $localeCode)
61
            ->setParameter('sectionCode', $sectionCode)
62
            ->setParameter('channelCode', $channelCode)
63
            ->getQuery()
64
            ->getResult()
65
            ;
66
    }
67
68
    public function findByProductCode(
69
        string $productCode,
70
        string $localeCode,
71
        string $channelCode
72
    ): array {
73
        return $this->createQueryBuilder('o')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
74
            ->leftJoin('o.translations', 'translation')
75
            ->innerJoin('o.products', 'product')
76
            ->innerJoin('o.channels', 'channels')
77
            ->andWhere('translation.locale = :localeCode')
78
            ->andWhere('product.code = :productCode')
79
            ->andWhere('o.enabled = true')
80
            ->andWhere('channels.code = :channelCode')
81
            ->setParameter('localeCode', $localeCode)
82
            ->setParameter('productCode', $productCode)
83
            ->setParameter('channelCode', $channelCode)
84
            ->getQuery()
85
            ->getResult()
86
        ;
87
    }
88
}
89