SetPublicCookie.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestModule1\Controller\CookieTester;
  7. use \Magento\Framework\App\RequestInterface;
  8. /**
  9. */
  10. class SetPublicCookie extends \Magento\TestModule1\Controller\CookieTester
  11. {
  12. /**
  13. * Sets a public cookie with data from url parameters
  14. *
  15. * @return \Magento\Framework\App\ResponseInterface
  16. */
  17. public function execute()
  18. {
  19. $publicCookieMetadata = $this->getCookieMetadataFactory()->createPublicCookieMetadata();
  20. $cookieDomain = $this->request->getParam('cookie_domain');
  21. if ($cookieDomain !== null) {
  22. $publicCookieMetadata->setDomain($cookieDomain);
  23. }
  24. $cookiePath = $this->request->getParam('cookie_path');
  25. if ($cookiePath !== null) {
  26. $publicCookieMetadata->setPath($cookiePath);
  27. }
  28. $cookieDuration = $this->request->getParam('cookie_duration');
  29. if ($cookieDuration !== null) {
  30. $publicCookieMetadata->setDuration($cookieDuration);
  31. }
  32. $httpOnly = $this->request->getParam('cookie_httponly');
  33. if ($httpOnly !== null) {
  34. $publicCookieMetadata->setHttpOnly($httpOnly);
  35. }
  36. $secure = $this->request->getParam('cookie_secure');
  37. if ($secure !== null) {
  38. $publicCookieMetadata->setSecure($secure);
  39. }
  40. $cookieName = $this->request->getParam('cookie_name');
  41. $cookieValue = $this->request->getParam('cookie_value');
  42. $this->getCookieManager()->setPublicCookie($cookieName, $cookieValue, $publicCookieMetadata);
  43. return $this->_response;
  44. }
  45. }