|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Mageplaza |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Mageplaza.com license that is |
| 8 | + * available through the world-wide-web at this URL: |
| 9 | + * https://www.mageplaza.com/LICENSE.txt |
| 10 | + * |
| 11 | + * DISCLAIMER |
| 12 | + * |
| 13 | + * Do not edit or add to this file if you wish to upgrade this extension to newer |
| 14 | + * version in the future. |
| 15 | + * |
| 16 | + * @category Mageplaza |
| 17 | + * @package Mageplaza_StockStatusGraphQl |
| 18 | + * @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/) |
| 19 | + * @license https://www.mageplaza.com/LICENSE.txt |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace Mageplaza\StockStatusGraphQl\Model\Resolver\QuoteItem; |
| 25 | + |
| 26 | +use Magento\Framework\Exception\LocalizedException; |
| 27 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 28 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 29 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 30 | +use Mageplaza\StockStatus\Helper\Data; |
| 31 | +use Mageplaza\StockStatus\Model\Config\Source\PageType; |
| 32 | +use Mageplaza\StockStatus\Model\Config\Source\StatusFormat; |
| 33 | +use Magento\Quote\Model\Quote\Item as QuoteItem; |
| 34 | + |
| 35 | +/** |
| 36 | + * Class StockStatus |
| 37 | + * @package Mageplaza\StockStatusGraphQl\Model\Resolver\QuoteItem |
| 38 | + */ |
| 39 | +class StockStatus implements ResolverInterface |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @var Data |
| 43 | + */ |
| 44 | + protected $helperData; |
| 45 | + |
| 46 | + /** |
| 47 | + * StockStatus constructor. |
| 48 | + * @param Data $helperData |
| 49 | + */ |
| 50 | + public function __construct(Data $helperData) |
| 51 | + { |
| 52 | + $this->helperData = $helperData; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + public function resolve( |
| 59 | + Field $field, |
| 60 | + $context, |
| 61 | + ResolveInfo $info, |
| 62 | + array $value = null, |
| 63 | + array $args = null |
| 64 | + ) { |
| 65 | + if (!$this->helperData->isEnabled()) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + if (!isset($value['model'])) { |
| 70 | + throw new LocalizedException(__('"model" value should be specified')); |
| 71 | + } |
| 72 | + |
| 73 | + /** @var QuoteItem $quoteItem */ |
| 74 | + $quoteItem = $value['model']; |
| 75 | + |
| 76 | + $status = [StatusFormat::LABEL_IMAGE, StatusFormat::IMAGE_LABEL]; |
| 77 | + $imageAvailable = array_merge($status, [StatusFormat::ONLY_IMAGE]); |
| 78 | + $textAvailable = array_merge($status, [StatusFormat::ONLY_LABEL]); |
| 79 | + $format = (int)$this->helperData->getStatusFormat(); |
| 80 | + |
| 81 | + $product = $this->helperData->getProductData($quoteItem->getProduct()); |
| 82 | + $isDisplay = $this->helperData->isDisplayStatus( |
| 83 | + $product->isAvailable(), |
| 84 | + PageType::SHOPPING_CART |
| 85 | + ); |
| 86 | + $result = []; |
| 87 | + if ($isDisplay) { |
| 88 | + $stockStatus = $this->helperData->getStatus($product, false); |
| 89 | + |
| 90 | + if ($stockStatus['image'] && in_array($format, $imageAvailable, true)) { |
| 91 | + $result['image'] = $stockStatus['image']; |
| 92 | + } |
| 93 | + |
| 94 | + if ($stockStatus['label'] && in_array($format, $textAvailable, true)) { |
| 95 | + $result['label'] = $stockStatus['label']; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return $result; |
| 100 | + } |
| 101 | +} |
0 commit comments