11<?php
2-
2+ /**
3+ * Copyright © Magento, Inc. All rights reserved.
4+ * See COPYING.txt for license details.
5+ */
6+ declare (strict_types=1 );
37
48namespace Lof \ProductTagsGraphQl \Model \Resolver \DataProvider ;
59
10+ use Lof \ProductTags \Api \Data \TagProductLinkInterface ;
11+ use Lof \ProductTags \Model \Data \TagProductLink ;
12+ use Lof \ProductTags \Api \ProductsManagementInterface ;
13+ use Magento \Framework \Exception \NoSuchEntityException ;
14+
15+ /**
16+ * Lof Product Tags data provider
17+ */
18+
619class Product
720{
21+ /**
22+ * @var ProductsManagementInterface
23+ */
24+ private $ productsManagement ;
825
9- private $ tag ;
26+ /**
27+ * @var \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory
28+ */
29+ protected $ productLinkFactory ;
1030
1131 /**
12- * @param \Lof\ProductTags\Api\Data\TagInterface $tag
32+ * @param \Lof\ProductTags\Api\ProductsManagementInterface $productsManagement
33+ * @param \Lof\ProductTags\Model\TagFactory $tagModelFactory
34+ * @param \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory $productLinkFactory
1335 */
1436 public function __construct (
15- \Lof \ProductTags \Api \Data \TagInterface $ tag
37+ \Lof \ProductTags \Api \ProductsManagementInterface $ productsManagement ,
38+ \Lof \ProductTags \Model \TagFactory $ tagModelFactory ,
39+ \Lof \ProductTags \Api \Data \TagProductLinkInterfaceFactory $ productLinkFactory
1640 ) {
17- $ this ->tag = $ tag ;
41+ $ this ->productsManagement = $ productsManagement ;
42+ $ this ->_tagModelFactory = $ tagModelFactory ;
43+ $ this ->productLinkFactory = $ productLinkFactory ;
1844 }
1945
20- public function getProduct ()
46+ /**
47+ * Get product data
48+ *
49+ * @param string $tagCode
50+ * @return array
51+ * @throws NoSuchEntityException
52+ */
53+ public function getData (string $ tagCode ): array
2154 {
22- return 'proviced data ' ;
55+ $ tagModel = $ this ->_tagModelFactory ->create ();
56+ $ tagModel ->loadByIdentifier ($ tagCode );
57+ if (!$ tagModel ->getId ()) {
58+ return [];
59+ }
60+ /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
61+ $ products = $ tagModel ->getProductCollection ();
62+ /** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface[] $productsData */
63+ $ productsData = [];
64+ if ($ products ){
65+ /** @var \Magento\Catalog\Model\Product $product */
66+ foreach ($ products ->getItems () as $ product ) {
67+ /** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface $productData */
68+ $ productData = $ this ->productLinkFactory ->create ();
69+ $ productData ->setSku ($ product ->getSku ())
70+ ->setPosition ($ product ->getData ('tag_index_position ' ))
71+ ->setTagId ($ tagModel ->getId ());
72+ $ productData = [
73+ TagProductLinkInterface::KEY_SKU => $ product ->getSku (),
74+ TagProductLinkInterface::KEY_POSITION => $ product ->getPosition (),
75+ TagProductLinkInterface::KEY_TAG_ID => $ product ->getTagId (),
76+ ];
77+ // $productData->setSku($product->getSku())
78+ // ->setPosition($product->getData('tag_index_position'))
79+ // ->setTagId($tagModel->getId());
80+ $ productsData [] = $ productData ;
81+ return $ productsData ;
82+ }
83+ }
84+ // $product = $this->productsManagement->getProducts($tagCode);
85+
86+ // $productData = [
87+ // TagProductLinkInterface::KEY_SKU => $product->getSku(),
88+ // TagProductLinkInterface::KEY_POSITION => $product->getPosition(),
89+ // TagProductLinkInterface::KEY_TAG_ID => $product->getTagId(),
90+ // ];
91+ // return $productData;
2392 }
2493}
0 commit comments