TitleHandler.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Kp\Gateway\Handler;
  11. use Magento\Payment\Gateway\Config\ValueHandlerInterface;
  12. use Magento\Payment\Model\InfoInterface;
  13. /**
  14. * Class TitleHandler
  15. *
  16. * @package Klarna\Kp\Gateway\Handler
  17. */
  18. class TitleHandler implements ValueHandlerInterface
  19. {
  20. const DEFAULT_TITLE = 'Klarna Payments';
  21. const DEFAULT_TITLE_FORMAT = '%s (%s)';
  22. /**
  23. * Retrieve method configured value
  24. *
  25. * @param array $subject
  26. * @param int|null $storeId
  27. *
  28. * @return mixed
  29. * @SuppressWarnings(PMD.UnusedFormalParameter)
  30. */
  31. public function handle(array $subject, $storeId = null)
  32. {
  33. if (!isset($subject['payment'])) {
  34. return self::DEFAULT_TITLE;
  35. }
  36. /** @var InfoInterface $payment */
  37. $payment = $subject['payment']->getPayment();
  38. $title = $this->getTitle($payment);
  39. return $title;
  40. }
  41. /**
  42. * Get title for specified payment method
  43. *
  44. * @param InfoInterface $payment
  45. * @return string
  46. */
  47. public function getTitle($payment)
  48. {
  49. if ($payment->hasAdditionalInformation('method_title')) {
  50. return $payment->getAdditionalInformation('method_title');
  51. }
  52. if ($payment->hasAdditionalInformation('method_code')) {
  53. return sprintf(
  54. self::DEFAULT_TITLE_FORMAT,
  55. self::DEFAULT_TITLE,
  56. $payment->hasAdditionalInformation('method_code')
  57. );
  58. }
  59. return self::DEFAULT_TITLE;
  60. }
  61. }