Completed
Push — master ( 159a25...a86692 )
by Stefan
03:09
created

ProductSearch::getProductFromConditionStream()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 4
nop 1
dl 0
loc 31
rs 8.8571
c 2
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Components\ProductStream;
9
10
use Shopware\Models\ProductStream\ProductStream;
11
use Shopware\Bundle\SearchBundle\ProductSearchResult;
12
use Shopware\Bundle\SearchBundle\Criteria;
13
use Shopware\Bundle\SearchBundle\Condition\CustomerGroupCondition;
14
use Shopware\Bundle\SearchBundle\Condition\CategoryCondition;
15
use Shopware\Bundle\SearchBundle\ProductSearchInterface;
16
use Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface;
17
use ShopwarePlugins\Connect\Components\Config;
18
use Shopware\Bundle\StoreFrontBundle\Struct\ShopContext;
19
use Shopware\Components\ProductStream\Repository as StreamRepository;
20
21
class ProductSearch
22
{
23
    /**
24
     * @var StreamRepository
25
     */
26
    private $productStreamRepository;
27
28
    /**
29
     * @var Config
30
     */
31
    private $config;
32
33
    /**
34
     * @var ProductSearchInterface
35
     */
36
    private $productSearchService;
37
38
    /**
39
     * @var ContextServiceInterface
40
     */
41
    private $contextService;
42
43
    public function __construct(
44
        StreamRepository $productStreamRepository,
45
        Config $config,
46
        ProductSearchInterface $productSearchService,
47
        ContextServiceInterface $contextService
48
    ) {
49
        $this->productStreamRepository = $productStreamRepository;
50
        $this->config = $config;
51
        $this->productSearchService = $productSearchService;
52
        $this->contextService = $contextService;
53
    }
54
55
    /**
56
     * @param ProductStream $stream
57
     * @return ProductSearchResult
58
     */
59
    public function getProductFromConditionStream(ProductStream $stream)
60
    {
61
        $criteria = new Criteria();
62
63
        $conditions = json_decode($stream->getConditions(), true);
64
        $conditions = $this->productStreamRepository->unserialize($conditions);
65
66
        foreach ($conditions as $condition) {
67
            $criteria->addCondition($condition);
68
        }
69
70
        $sorting = json_decode($stream->getSorting(), true);
71
        $sorting = $this->productStreamRepository->unserialize($sorting);
72
73
        foreach ($sorting as $sort) {
74
            $criteria->addSorting($sort);
75
        }
76
77
        /** @var ShopContext $context */
78
        $context = $this->contextService->createShopContext($this->config->getDefaultShopId());
79
80
        $criteria->addBaseCondition(
81
            new CustomerGroupCondition([$context->getCurrentCustomerGroup()->getId()])
82
        );
83
84
        $criteria->addBaseCondition(
85
            new CategoryCondition([$context->getShop()->getCategory()->getId()])
86
        );
87
88
        return $this->productSearchService->search($criteria, $context);
89
    }
90
}
91