CustomerScopeData.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Class CustomerScopeData provide scope (website, store or store_group) information on front
  11. * Can be used, for example, on store front, in order to determine
  12. * that private cache invalid for current scope, by comparing
  13. * with appropriate value in store front private cache.
  14. *
  15. * @api
  16. * @since 100.1.9
  17. */
  18. class CustomerScopeData extends \Magento\Framework\View\Element\Template
  19. {
  20. /**
  21. * @var \Magento\Framework\View\Element\Template\Context
  22. */
  23. private $storeManager;
  24. /**
  25. * @var Json
  26. */
  27. private $serializer;
  28. /**
  29. * @param \Magento\Framework\View\Element\Template\Context $context
  30. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  31. * @param array $data
  32. * @param Json|null $serializer
  33. * @throws \RuntimeException
  34. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Element\Template\Context $context,
  38. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  39. array $data = [],
  40. Json $serializer = null
  41. ) {
  42. parent::__construct($context, $data);
  43. $this->storeManager = $context->getStoreManager();
  44. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  45. }
  46. /**
  47. * Return id of current website
  48. *
  49. * Can be used when necessary to obtain website id of the current customer.
  50. *
  51. * @return integer
  52. * @since 100.1.9
  53. */
  54. public function getWebsiteId()
  55. {
  56. return (int)$this->_storeManager->getStore()->getWebsiteId();
  57. }
  58. /**
  59. * Encode invalidation rules.
  60. *
  61. * @param array $configuration
  62. * @return bool|string
  63. * @throws \InvalidArgumentException
  64. * @since 102.0.0
  65. */
  66. public function encodeConfiguration(array $configuration)
  67. {
  68. return $this->serializer->serialize($configuration);
  69. }
  70. }