Completed
Push — master ( b26fa6...142849 )
by
unknown
08:53
created

findByEnabledAndChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Odiseo Marketplace Plugin package.
5
 *
6
 * Copyright (c) 2018-2020 Odiseo Team - Diego D'amico
7
 */
8
9
declare(strict_types=1);
10
11
namespace Odiseo\SyliusVendorPlugin\Repository;
12
13
use Doctrine\ORM\QueryBuilder;
14
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface;
15
use Sylius\Component\Core\Model\ChannelInterface;
16
17
trait BaseProductRepositoryTrait
18
{
19
    /**
20
     * @param $alias
21
     * @param null $indexBy
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $indexBy is correct as it would always require null to be passed?
Loading history...
22
     * @return mixed
23
     */
24
    abstract public function createQueryBuilder($alias, $indexBy = null);
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function createListByVendorQueryBuilder(VendorInterface $vendor, string $locale, $taxonId = null): QueryBuilder
30
    {
31
        $queryBuilder = $this->createQueryBuilder('o')
32
            ->addSelect('translation')
33
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
34
            ->andWhere('o.vendor = :vendor')
35
            ->setParameter('locale', $locale)
36
            ->setParameter('vendor', $vendor)
37
        ;
38
39
        if (null !== $taxonId) {
40
            $queryBuilder
41
                ->innerJoin('o.productTaxons', 'productTaxon')
42
                ->andWhere('productTaxon.taxon = :taxonId')
43
                ->setParameter('taxonId', $taxonId)
44
            ;
45
        }
46
47
        return $queryBuilder;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function findByVendorAndNamePart(VendorInterface $vendor, string $phrase, string $locale, ?int $limit = null): array
54
    {
55
        return $this->createQueryBuilder('o')
56
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
57
            ->andWhere('o.vendor = :vendor')
58
            ->andWhere('translation.name LIKE :name')
59
            ->setParameter('vendor', $vendor)
60
            ->setParameter('name', '%' . $phrase . '%')
61
            ->setParameter('locale', $locale)
62
            ->setMaxResults($limit)
63
            ->getQuery()
64
            ->getResult()
65
        ;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function findByEnabledAndChannel(VendorInterface $vendor, ChannelInterface $channel): array
72
    {
73
        return $this->createQueryBuilder('o')
74
            ->andWhere('o.vendor = :vendor')
75
            ->andWhere('o.enabled = true')
76
            ->andWhere(':channel MEMBER OF o.channels')
77
            ->setParameter('vendor', $vendor)
78
            ->setParameter('channel', $channel)
79
            ->getQuery()
80
            ->getResult()
81
            ;
82
    }
83
}
84