CcConfigProvider.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Framework\View\Asset\Source;
  9. /**
  10. * Class CcConfigProvider
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class CcConfigProvider implements ConfigProviderInterface
  16. {
  17. /**
  18. * @var array
  19. */
  20. private $icons = [];
  21. /**
  22. * @var CcConfig
  23. */
  24. protected $ccConfig;
  25. /**
  26. * @var \Magento\Framework\View\Asset\Source
  27. */
  28. protected $assetSource;
  29. /**
  30. * @param CcConfig $ccConfig
  31. * @param Source $assetSource
  32. */
  33. public function __construct(
  34. CcConfig $ccConfig,
  35. Source $assetSource
  36. ) {
  37. $this->ccConfig = $ccConfig;
  38. $this->assetSource = $assetSource;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getConfig()
  44. {
  45. return [
  46. 'payment' => [
  47. 'ccform' => [
  48. 'icons' => $this->getIcons()
  49. ]
  50. ]
  51. ];
  52. }
  53. /**
  54. * Get icons for available payment methods
  55. *
  56. * @return array
  57. */
  58. public function getIcons()
  59. {
  60. if (!empty($this->icons)) {
  61. return $this->icons;
  62. }
  63. $types = $this->ccConfig->getCcAvailableTypes();
  64. foreach (array_keys($types) as $code) {
  65. if (!array_key_exists($code, $this->icons)) {
  66. $asset = $this->ccConfig->createAsset('Magento_Payment::images/cc/' . strtolower($code) . '.png');
  67. $placeholder = $this->assetSource->findSource($asset);
  68. if ($placeholder) {
  69. list($width, $height) = getimagesize($asset->getSourceFile());
  70. $this->icons[$code] = [
  71. 'url' => $asset->getUrl(),
  72. 'width' => $width,
  73. 'height' => $height
  74. ];
  75. }
  76. }
  77. }
  78. return $this->icons;
  79. }
  80. }