Completed
Push — master ( cf353c...bbc3bb )
by Mikołaj
11s
created

MediaRepository::findOneEnabledByCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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): ?MediaInterface
29
    {
30
        return $this->createQueryBuilder('o')
31
            ->where('o.code = :code')
32
            ->andWhere('o.enabled = true')
33
            ->setParameter('code', $code)
34
            ->getQuery()
35
            ->getOneOrNullResult()
36
        ;
37
    }
38
39
    public function findBySectionCode(string $sectionCode, string $localeCode): array
40
    {
41
        return $this->createQueryBuilder('o')
42
            ->leftJoin('o.translations', 'translation')
43
            ->innerJoin('o.sections', 'section')
44
            ->andWhere('translation.locale = :localeCode')
45
            ->andWhere('section.code = :sectionCode')
46
            ->andWhere('o.enabled = true')
47
            ->setParameter('localeCode', $localeCode)
48
            ->setParameter('sectionCode', $sectionCode)
49
            ->getQuery()
50
            ->getResult()
51
        ;
52
    }
53
54
    public function findByProductCode(string $productCode, string $localeCode): array
55
    {
56
        return $this->createQueryBuilder('o')
57
            ->leftJoin('o.translations', 'translation')
58
            ->innerJoin('o.products', 'product')
59
            ->andWhere('translation.locale = :localeCode')
60
            ->andWhere('product.code = :productCode')
61
            ->andWhere('o.enabled = true')
62
            ->setParameter('localeCode', $localeCode)
63
            ->setParameter('productCode', $productCode)
64
            ->getQuery()
65
            ->getResult()
66
        ;
67
    }
68
}
69