Passed
Push — master ( 712bd4...f29127 )
by Maxime
13:13
created

ProductRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createProductListByProductCodes() 0 24 1
A createProductListByTaxonCode() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MonsieurBiz\SyliusRichEditorPlugin\Repository;
6
7
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as SyliusProductRepository;
8
use Sylius\Component\Core\Model\ChannelInterface;
9
use Sylius\Component\Core\Model\TaxonInterface;
10
11
class ProductRepository extends SyliusProductRepository
12
{
13
    /**
14
     * Get available products depending on a list of codes
15
     *
16
     * @param string $products
17
     * @param ChannelInterface $channel
18
     * @param string $locale
19
     * @return array
20
     */
21
    public function createProductListByProductCodes(string $products, ChannelInterface $channel, string $locale): array
22
    {
23
        // Add translation
24
        $queryBuilder = $this->createQueryBuilder('o')
25
            ->distinct()
26
            ->addSelect('translation')
27
            ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
28
        ;
29
30
        // Filter on channel, enabled and locale
31
        $queryBuilder
32
            ->andWhere(':channel MEMBER OF o.channels')
33
            ->andWhere('o.enabled = true')
34
            ->setParameter('locale', $locale)
35
            ->setParameter('channel', $channel)
36
        ;
37
38
        // Filter on product codes
39
        $queryBuilder
40
            ->andWhere('o.code IN (:productCodes)')
41
            ->setParameter('productCodes', explode(',', $products))
42
        ;
43
44
        return $queryBuilder->getQuery()->getResult();
45
    }
46
47
    /**
48
     * Get product list from Taxon
49
     *
50
     * @param ChannelInterface $channel
51
     * @param TaxonInterface $taxon
52
     * @param string $locale
53
     * @param array $sorting
54
     * @param bool $includeAllDescendants
55
     * @param int $count
56
     * @return array
57
     */
58
    public function createProductListByTaxonCode(
59
        ChannelInterface $channel,
60
        TaxonInterface $taxon,
61
        string $locale,
62
        array $sorting = [],
63
        bool $includeAllDescendants = false,
64
        string $count
65
    ): array {
66
        $queryBuilder = parent::createShopListQueryBuilder($channel, $taxon, $locale, $sorting, $includeAllDescendants);
67
        $queryBuilder->setMaxResults((int) $count);
68
69
        return $queryBuilder->getQuery()->getResult();
70
71
    }
72
}
73