Completed
Pull Request — master (#398)
by Stefan
02:43
created

ProductSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B getProductFromConditionStream() 0 31 3
1
<?php
2
3
namespace ShopwarePlugins\Connect\Components\ProductStream;
4
5
use Shopware\Models\ProductStream\ProductStream;
6
use Shopware\Bundle\SearchBundle\ProductSearchResult;
7
use Shopware\Bundle\SearchBundle\Criteria;
8
use Shopware\Bundle\SearchBundle\Condition\CustomerGroupCondition;
9
use Shopware\Bundle\SearchBundle\Condition\CategoryCondition;
10
use Shopware\Bundle\SearchBundle\ProductSearchInterface;
11
use Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface;
12
use ShopwarePlugins\Connect\Components\Config;
13
use Shopware\Bundle\StoreFrontBundle\Struct\ShopContext;
14
use Shopware\Components\ProductStream\Repository as StreamRepository;
15
16
class ProductSearch
17
{
18
    /**
19
     * @var StreamRepository
20
     */
21
    private $productStreamRepository;
22
23
    /**
24
     * @var Config
25
     */
26
    private $config;
27
28
    /**
29
     * @var ProductSearchInterface
30
     */
31
    private $productSearchService;
32
33
    /**
34
     * @var ContextServiceInterface
35
     */
36
    private $contextService;
37
38
    public function __construct(
39
        StreamRepository $productStreamRepository,
40
        Config $config,
41
        ProductSearchInterface $productSearchService,
42
        ContextServiceInterface $contextService
43
    )
44
    {
45
        $this->productStreamRepository = $productStreamRepository;
46
        $this->config = $config;
47
        $this->productSearchService = $productSearchService;
48
        $this->contextService = $contextService;
49
    }
50
51
    /**
52
     * @param ProductStream $stream
53
     * @return ProductSearchResult
54
     */
55
    public function getProductFromConditionStream(ProductStream $stream)
56
    {
57
        $criteria = new Criteria();
58
59
        $conditions = json_decode($stream->getConditions(), true);
60
        $conditions = $this->productStreamRepository->unserialize($conditions);
61
62
        foreach ($conditions as $condition) {
63
            $criteria->addCondition($condition);
64
        }
65
66
        $sorting = json_decode($stream->getSorting(), true);
67
        $sorting = $this->productStreamRepository->unserialize($sorting);
68
69
        foreach ($sorting as $sort) {
70
            $criteria->addSorting($sort);
71
        }
72
73
        /** @var ShopContext $context */
74
        $context = $this->contextService->createShopContext($this->config->getDefaultShopId());
75
76
        $criteria->addBaseCondition(
77
            new CustomerGroupCondition([$context->getCurrentCustomerGroup()->getId()])
78
        );
79
80
        $criteria->addBaseCondition(
81
            new CategoryCondition([$context->getShop()->getCategory()->getId()])
82
        );
83
84
        return $this->productSearchService->search($criteria, $context);
85
    }
86
}