ProductRepositoryTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 33
dl 0
loc 60
rs 10
c 1
b 1
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A createShopListByVendorQueryBuilder() 0 48 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusVendorPlugin\Repository;
6
7
use Doctrine\ORM\QueryBuilder;
8
use Odiseo\SyliusVendorPlugin\Entity\VendorInterface;
9
use Sylius\Component\Core\Model\ChannelInterface;
10
11
trait ProductRepositoryTrait
12
{
13
    /**
14
     * @param $alias
15
     * @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...
16
     * @return mixed
17
     */
18
    abstract public function createQueryBuilder($alias, $indexBy = null);
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function createShopListByVendorQueryBuilder(
24
        ChannelInterface $channel,
25
        VendorInterface $vendor,
26
        string $locale,
27
        array $sorting = []
28
    ): QueryBuilder {
29
        $queryBuilder = $this->createQueryBuilder('o')
30
            ->distinct()
31
            ->addSelect('translation')
32
            ->addSelect('productTaxon')
33
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
34
            ->innerJoin('o.productTaxons', 'productTaxon');
35
36
        $queryBuilder
37
            ->andWhere(':channel MEMBER OF o.channels')
38
            ->andWhere('o.enabled = true')
39
            ->andWhere('o.vendor = :vendor')
40
            ->setParameter('locale', $locale)
41
            ->setParameter('channel', $channel)
42
            ->setParameter('vendor', $vendor)
43
        ;
44
45
        if (isset($sorting['price'])) {
46
            $subQuery = $this->createQueryBuilder('m')
47
                ->select('min(v.position)')
48
                ->innerJoin('m.variants', 'v')
49
                ->andWhere('m.id = :product_id')
50
                ->andWhere('v.enabled = :enabled')
51
            ;
52
53
            $queryBuilder
54
                ->addSelect('variant')
55
                ->addSelect('channelPricing')
56
                ->innerJoin('o.variants', 'variant')
57
                ->innerJoin('variant.channelPricings', 'channelPricing')
58
                ->andWhere('channelPricing.channelCode = :channelCode')
59
                ->andWhere(
60
                    $queryBuilder->expr()->in(
61
                        'variant.position',
62
                        str_replace(':product_id', 'o.id', $subQuery->getDQL())
63
                    )
64
                )
65
                ->setParameter('channelCode', $channel->getCode())
66
                ->setParameter('enabled', true)
67
            ;
68
        }
69
70
        return $queryBuilder;
71
    }
72
}
73