SensitiveCookieMetadata.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\Cookie;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * Class SensitiveCookieMetadata
  10. *
  11. * The class has only methods extended from CookieMetadata
  12. * as path and domain are only data to be exposed by SensitiveCookieMetadata
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class SensitiveCookieMetadata extends CookieMetadata
  18. {
  19. /**
  20. * @var RequestInterface
  21. */
  22. protected $request;
  23. /**
  24. * @param RequestInterface $request
  25. * @param array $metadata
  26. */
  27. public function __construct(RequestInterface $request, $metadata = [])
  28. {
  29. if (!isset($metadata[self::KEY_HTTP_ONLY])) {
  30. $metadata[self::KEY_HTTP_ONLY] = true;
  31. }
  32. $this->request = $request;
  33. parent::__construct($metadata);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getSecure()
  39. {
  40. $this->updateSecureValue();
  41. return $this->get(self::KEY_SECURE);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __toArray()
  47. {
  48. $this->updateSecureValue();
  49. return parent::__toArray();
  50. }
  51. /**
  52. * Update secure value, set it to request setting if it has no explicit value assigned.
  53. *
  54. * @return void
  55. */
  56. private function updateSecureValue()
  57. {
  58. if (null === $this->get(self::KEY_SECURE)) {
  59. $this->set(self::KEY_SECURE, $this->request->isSecure());
  60. }
  61. }
  62. }