Identifier.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\CustomerData\Section;
  7. /**
  8. * Customer section identifier
  9. */
  10. class Identifier
  11. {
  12. const COOKIE_KEY = 'storage_data_id';
  13. const SECTION_KEY = 'data_id';
  14. const UPDATE_MARK = 'sections_updated';
  15. /**
  16. * @var int
  17. */
  18. protected $markId;
  19. /**
  20. * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
  21. */
  22. protected $cookieManager;
  23. /**
  24. * @var \Magento\Framework\Session\Config\ConfigInterface
  25. */
  26. protected $sessionConfig;
  27. /**
  28. * @param \Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager
  29. */
  30. public function __construct(
  31. \Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager
  32. ) {
  33. $this->cookieManager = $cookieManager;
  34. }
  35. /**
  36. * Init mark(identifier) for sections
  37. *
  38. * @param bool $forceNewTimestamp
  39. * @return int
  40. */
  41. public function initMark($forceNewTimestamp)
  42. {
  43. if ($forceNewTimestamp) {
  44. $this->markId = time();
  45. return $this->markId;
  46. }
  47. $cookieMarkId = false;
  48. if (!$this->markId) {
  49. $cookieMarkId = $this->cookieManager->getCookie(self::COOKIE_KEY);
  50. }
  51. $this->markId = $cookieMarkId ? $cookieMarkId : time();
  52. return $this->markId;
  53. }
  54. /**
  55. * Mark sections with data id
  56. *
  57. * @param array $sectionsData
  58. * @param array|null $sectionNames
  59. * @param bool $forceNewTimestamp
  60. * @return array
  61. */
  62. public function markSections(array $sectionsData, $sectionNames = null, $forceNewTimestamp = false)
  63. {
  64. if (!$sectionNames) {
  65. $sectionNames = array_keys($sectionsData);
  66. }
  67. $markId = $this->initMark($forceNewTimestamp);
  68. foreach ($sectionNames as $name) {
  69. if ($forceNewTimestamp || !array_key_exists(self::SECTION_KEY, $sectionsData[$name])) {
  70. $sectionsData[$name][self::SECTION_KEY] = $markId;
  71. }
  72. }
  73. return $sectionsData;
  74. }
  75. }