Skip to content

Commit 1b92aca

Browse files
author
b__b
committed
refactoring
1 parent b5bdaf7 commit 1b92aca

File tree

11 files changed

+374
-121
lines changed

11 files changed

+374
-121
lines changed

Controller/Label/Get.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\ProductLabel\Controller\Label;
9+
10+
use Magento\Framework\App\Action\Context;
11+
use Magento\Framework\Controller\Result\JsonFactory;
12+
use Magento\Framework\View\Result\PageFactory;
13+
use Magento\Catalog\Model\Product;
14+
use Magefan\ProductLabel\Model\GetLabels;
15+
16+
class Get extends \Magento\Framework\App\Action\Action
17+
{
18+
/**
19+
* @var JsonFactory
20+
*/
21+
protected $jsonResultFactory;
22+
23+
/**
24+
* @var GetLabels
25+
*/
26+
protected $getLabels;
27+
28+
/**
29+
* Get constructor.
30+
* @param Context $context
31+
* @param JsonFactory $jsonResultFactory
32+
* @param PageFactory $resultPageFactory
33+
* @param GetLabels $getLabels
34+
*/
35+
public function __construct(
36+
Context $context,
37+
JsonFactory $jsonResultFactory,
38+
GetLabels $getLabels
39+
) {
40+
parent::__construct($context);
41+
$this->jsonResultFactory = $jsonResultFactory;
42+
$this->getLabels = $getLabels;
43+
}
44+
45+
public function execute()
46+
{
47+
$result = $this->jsonResultFactory->create();
48+
$replaceMap = $this->getLabels->execute($this->getProductIds(), $this->getProductIdsForProductPage());
49+
50+
$result->setData([
51+
'labels' => $replaceMap
52+
]);
53+
54+
$result->setHeader('X-Magento-Tags', implode(',', $this->getCacheTags()));
55+
56+
return $result;
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
private function getCacheTags(): array
63+
{
64+
$tags = [];
65+
66+
foreach ($this->getProductIds() as $productId) {
67+
$tags[] = Product::CACHE_TAG . '_' . $productId;
68+
}
69+
70+
return $tags;
71+
}
72+
73+
/**
74+
* Retrieve and process product IDs from the request.
75+
*
76+
* @param string $paramName
77+
* @return array
78+
*/
79+
private function getProductIdsFromRequest(string $paramName): array
80+
{
81+
$productIds = (string) $this->getRequest()->getParam($paramName, '');
82+
83+
return array_map('intval', explode(',', $productIds));
84+
}
85+
86+
/**
87+
* @return array
88+
*/
89+
private function getProductIds(): array
90+
{
91+
return $this->getProductIdsFromRequest('product_ids');
92+
}
93+
94+
/**
95+
* @return array
96+
*/
97+
private function getProductIdsForProductPage(): array
98+
{
99+
return $this->getProductIdsFromRequest('product_ids_for_product_page');
100+
}
101+
}

Model/GetLabels.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magefan\ProductLabel\Model;
9+
10+
use Magefan\ProductLabel\Model\Config;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap;
13+
use Magefan\ProductLabel\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory;
14+
use Magefan\ProductLabel\Api\LabelProcessorInterface;
15+
use Magento\Framework\View\LayoutInterface;
16+
17+
class GetLabels
18+
{
19+
/**
20+
* @var Config
21+
*/
22+
protected $config;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
protected $storeManager;
28+
29+
/**
30+
* @var GetProductIdsToRuleIdsMap
31+
*/
32+
protected $getProductIdsToRuleIdsMap;
33+
34+
/**
35+
* @var RuleCollectionFactory
36+
*/
37+
protected $ruleCollectionFactory;
38+
39+
/**
40+
* @var LabelProcessorInterface|null
41+
*/
42+
protected $labelProcessor;
43+
44+
/**
45+
* @var LayoutInterface
46+
*/
47+
protected $layout;
48+
49+
/**
50+
* GetLabels constructor.
51+
* @param StoreManagerInterface $storeManager
52+
* @param \Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap
53+
* @param RuleCollectionFactory $ruleCollectionFactory
54+
* @param LayoutInterface $layout
55+
* @param LabelProcessorInterface|null $labelProcessor
56+
*/
57+
public function __construct(
58+
Config $config,
59+
StoreManagerInterface $storeManager,
60+
GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap,
61+
RuleCollectionFactory $ruleCollectionFactory,
62+
LayoutInterface $layout,
63+
?LabelProcessorInterface $labelProcessor = null
64+
) {
65+
$this->config = $config;
66+
$this->storeManager = $storeManager;
67+
$this->getProductIdsToRuleIdsMap = $getProductIdsToRuleIdsMap;
68+
$this->ruleCollectionFactory = $ruleCollectionFactory;
69+
$this->labelProcessor = $labelProcessor;
70+
$this->layout = $layout;
71+
}
72+
73+
/**
74+
* @param array $productIds
75+
* @param $idsUseForProductPage
76+
* @return array
77+
*/
78+
public function execute(array $productIds, $productIdsForProductPage = []): array
79+
{
80+
[$ruleIds, $productIdRuleIds] = $this->getProductIdsToRuleIdsMap->execute($productIds);
81+
82+
$rules = $this->ruleCollectionFactory->create()
83+
->addActiveFilter()
84+
->addFieldToFilter('id', ['in' => $ruleIds])
85+
->addStoreFilter($this->storeManager->getStore()->getId())
86+
->setOrder('priority', 'asc');
87+
88+
$replaceMap = [];
89+
90+
foreach ($productIdRuleIds as $productId => $productRuleIds) {
91+
$forProductPage = in_array($productId, $productIdsForProductPage);
92+
93+
$productLabels = $this->getAvailableProductLabels($rules, $productRuleIds, $forProductPage);
94+
95+
$htmlToReplace = $this->layout
96+
->createBlock(\Magefan\ProductLabel\Block\Label::class)
97+
->setProductLabels($productLabels)
98+
->toHtml();
99+
100+
if ($htmlToReplace) {
101+
$replaceMap[$productId] = $htmlToReplace;
102+
}
103+
}
104+
105+
if (null !== $this->labelProcessor) {
106+
$replaceMap = $this->labelProcessor->execute($replaceMap, $productIds);
107+
}
108+
109+
return $replaceMap;
110+
}
111+
112+
/**
113+
* @param $rules
114+
* @param $productRuleIds
115+
* @param bool $forProductPage
116+
* @return array
117+
*/
118+
public function getAvailableProductLabels($rules, $productRuleIds, $forProductPage = false): array
119+
{
120+
$productLabelsCount = 0;
121+
$productLabels = [];
122+
123+
$discardRulePerPosition = [];
124+
125+
foreach ($rules as $rule) {
126+
if (in_array($rule->getId(), $productRuleIds)) {
127+
$productLabelsCount++;
128+
129+
if ($forProductPage) {
130+
$position = $rule->getPpPosition() ?: $rule->getPosition();
131+
} else {
132+
$position = $rule->getPosition() ?: 'top-left';
133+
}
134+
135+
if (!isset($discardRulePerPosition[$position])) {
136+
$productLabels[$position][] = $rule->getLabelData($forProductPage);
137+
}
138+
139+
if ($rule->getDiscardSubsequentRules()) {
140+
$discardRulePerPosition[$position] = true;
141+
}
142+
}
143+
144+
if ($productLabelsCount >= $this->config->getLabelsPerProduct()) {
145+
break;
146+
}
147+
}
148+
149+
return $productLabels;
150+
}
151+
}

0 commit comments

Comments
 (0)