PaymentTokens.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Block\Customer;
  7. use Magento\Framework\View\Element\Template;
  8. use Magento\Vault\Api\Data\PaymentTokenInterface;
  9. use Magento\Vault\Block\TokenRendererInterface;
  10. use Magento\Vault\Model\CustomerTokenManagement;
  11. /**
  12. * Class PaymentTokens
  13. */
  14. abstract class PaymentTokens extends Template
  15. {
  16. /**
  17. * @var PaymentTokenInterface[]
  18. */
  19. private $customerTokens;
  20. /**
  21. * @var CustomerTokenManagement
  22. */
  23. private $customerTokenManagement;
  24. /**
  25. * PaymentTokens constructor.
  26. * @param Template\Context $context
  27. * @param CustomerTokenManagement $customerTokenManagement
  28. * @param array $data
  29. */
  30. public function __construct(
  31. Template\Context $context,
  32. CustomerTokenManagement $customerTokenManagement,
  33. array $data = []
  34. ) {
  35. parent::__construct($context, $data);
  36. $this->customerTokenManagement = $customerTokenManagement;
  37. }
  38. /**
  39. * Get type of token
  40. * @return string
  41. */
  42. abstract public function getType();
  43. /**
  44. * @return PaymentTokenInterface[]
  45. */
  46. public function getPaymentTokens()
  47. {
  48. $tokens = [];
  49. /** @var PaymentTokenInterface $token */
  50. foreach ($this->getCustomerTokens() as $token) {
  51. if ($token->getType() === $this->getType()) {
  52. $tokens[] = $token;
  53. }
  54. }
  55. return $tokens;
  56. }
  57. /**
  58. * @param PaymentTokenInterface $token
  59. * @return string
  60. */
  61. public function renderTokenHtml(PaymentTokenInterface $token)
  62. {
  63. foreach ($this->getChildNames() as $childName) {
  64. $childBlock = $this->getChildBlock($childName);
  65. if ($childBlock instanceof TokenRendererInterface && $childBlock->canRender($token)) {
  66. return $childBlock->render($token);
  67. }
  68. }
  69. return '';
  70. }
  71. /**
  72. * Checks if customer tokens exists
  73. * @return bool
  74. */
  75. public function isExistsCustomerTokens()
  76. {
  77. return !empty($this->getCustomerTokens());
  78. }
  79. /**
  80. * Get customer session tokens
  81. * @return PaymentTokenInterface[]
  82. */
  83. private function getCustomerTokens()
  84. {
  85. if (empty($this->customerTokens)) {
  86. $this->customerTokens = $this->customerTokenManagement->getCustomerSessionTokens();
  87. }
  88. return $this->customerTokens;
  89. }
  90. }