IsSubscribed.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CustomerGraphQl\Model\Resolver;
  8. use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
  9. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  10. use Magento\Framework\GraphQl\Config\Element\Field;
  11. use Magento\Framework\GraphQl\Query\ResolverInterface;
  12. use Magento\Newsletter\Model\SubscriberFactory;
  13. /**
  14. * Customer is_subscribed field resolver
  15. */
  16. class IsSubscribed implements ResolverInterface
  17. {
  18. /**
  19. * @var CheckCustomerAccount
  20. */
  21. private $checkCustomerAccount;
  22. /**
  23. * @var SubscriberFactory
  24. */
  25. private $subscriberFactory;
  26. /**
  27. * @param CheckCustomerAccount $checkCustomerAccount
  28. * @param SubscriberFactory $subscriberFactory
  29. */
  30. public function __construct(
  31. CheckCustomerAccount $checkCustomerAccount,
  32. SubscriberFactory $subscriberFactory
  33. ) {
  34. $this->checkCustomerAccount = $checkCustomerAccount;
  35. $this->subscriberFactory = $subscriberFactory;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function resolve(
  41. Field $field,
  42. $context,
  43. ResolveInfo $info,
  44. array $value = null,
  45. array $args = null
  46. ) {
  47. $currentUserId = $context->getUserId();
  48. $currentUserType = $context->getUserType();
  49. $this->checkCustomerAccount->execute($currentUserId, $currentUserType);
  50. $status = $this->subscriberFactory->create()->loadByCustomerId((int)$currentUserId)->isSubscribed();
  51. return (bool)$status;
  52. }
  53. }