Skip to content

Commit 27aea35

Browse files
authored
Merge pull request #13 from yuriyhamulevych/12453-graph-ql-support
12453 graph ql support
2 parents c0d9c7a + 7ee550f commit 27aea35

File tree

7 files changed

+191
-154
lines changed

7 files changed

+191
-154
lines changed

Api/Data/RuleInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface RuleInterface extends \Magento\Framework\Api\ExtensibleDataInterface
1515
{
1616
/**
1717
* Get name
18-
* @return string|null
18+
* @return mixed
1919
*/
2020
public function getLabelData();
2121
}

Api/GetLabelsInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Api;
9+
10+
interface GetLabelsInterface
11+
{
12+
/**
13+
* @param array $productIds
14+
* @param $idsUseForProductPage
15+
* @return array
16+
*/
17+
public function execute(array $productIds, array $productIdsForProductPage = []): array;
18+
19+
/**
20+
* @param $rules
21+
* @param $productRuleIds
22+
* @param bool $forProductPage
23+
* @return array
24+
*/
25+
public function getAvailableProductLabels($rules, $productRuleIds, bool $forProductPage = false): array;
26+
}

Model/GetLabels.php

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

Model/Parser/Html.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct(
3030
/**
3131
* @param string $output
3232
* @return string
33+
* @throws \Magento\Framework\Exception\NoSuchEntityException
3334
*/
3435
public function execute(string $output): string
3536
{

Model/Rule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function getOwnTitle(bool $plural = false): string
8181
/**
8282
* Retrieve image url
8383
* @return string
84+
* @throws \Magento\Framework\Exception\NoSuchEntityException
8485
*/
8586
public function getImageUrl()
8687
{
@@ -100,6 +101,7 @@ public function getImageUrl()
100101
* Retrieve media url
101102
* @param string $file
102103
* @return string
104+
* @throws \Magento\Framework\Exception\NoSuchEntityException
103105
*/
104106
public function getMediaUrl(string $file): string
105107
{
@@ -108,7 +110,8 @@ public function getMediaUrl(string $file): string
108110
}
109111

110112
/**
111-
* @return string
113+
* @return array
114+
* @throws \Magento\Framework\Exception\NoSuchEntityException
112115
*/
113116
public function getLabelData(): array
114117
{

Plugin/Frontend/Magento/Framework/Controller/ResultInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class ResultInterface
3030
private $request;
3131

3232
/**
33+
* ResultInterface constructor.
3334
* @param Html $htmlParser
3435
* @param Config $config
36+
* @param \Magento\Framework\App\RequestInterface $request
3537
*/
3638
public function __construct(
3739
Html $htmlParser,
@@ -49,6 +51,7 @@ public function __construct(
4951
* @param Subject $result
5052
* @param ResponseInterface $response
5153
* @return Subject
54+
* @throws \Magento\Framework\Exception\NoSuchEntityException
5255
*/
5356
public function afterRenderResult(
5457
Subject $subject,

0 commit comments

Comments
 (0)