AbstractStorage.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Model\Storage;
  7. use Magento\UrlRewrite\Model\StorageInterface;
  8. use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
  9. use Magento\Framework\Api\DataObjectHelper;
  10. /**
  11. * Abstract db storage
  12. */
  13. abstract class AbstractStorage implements StorageInterface
  14. {
  15. /**
  16. * @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory
  17. */
  18. protected $urlRewriteFactory;
  19. /**
  20. * @var \Magento\Framework\Api\DataObjectHelper
  21. */
  22. protected $dataObjectHelper;
  23. /**
  24. * @param UrlRewriteFactory $urlRewriteFactory
  25. * @param DataObjectHelper $dataObjectHelper
  26. */
  27. public function __construct(
  28. UrlRewriteFactory $urlRewriteFactory,
  29. DataObjectHelper $dataObjectHelper
  30. ) {
  31. $this->urlRewriteFactory = $urlRewriteFactory;
  32. $this->dataObjectHelper = $dataObjectHelper;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function findAllByData(array $data)
  38. {
  39. $rows = $this->doFindAllByData($data);
  40. $urlRewrites = [];
  41. foreach ($rows as $row) {
  42. $urlRewrites[] = $this->createUrlRewrite($row);
  43. }
  44. return $urlRewrites;
  45. }
  46. /**
  47. * Find all rows by specific filter. Template method
  48. *
  49. * @param array $data
  50. * @return array
  51. */
  52. abstract protected function doFindAllByData(array $data);
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function findOneByData(array $data)
  57. {
  58. $row = $this->doFindOneByData($data);
  59. return $row ? $this->createUrlRewrite($row) : null;
  60. }
  61. /**
  62. * Find row by specific filter. Template method
  63. *
  64. * @param array $data
  65. * @return array
  66. */
  67. abstract protected function doFindOneByData(array $data);
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function replace(array $urls)
  72. {
  73. if (!$urls) {
  74. return [];
  75. }
  76. return $this->doReplace($urls);
  77. }
  78. /**
  79. * Save new url rewrites and remove old if exist. Template method
  80. *
  81. * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls
  82. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
  83. * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException|\Exception
  84. */
  85. abstract protected function doReplace(array $urls);
  86. /**
  87. * Create url rewrite object
  88. *
  89. * @param array $data
  90. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite
  91. */
  92. protected function createUrlRewrite($data)
  93. {
  94. $dataObject = $this->urlRewriteFactory->create();
  95. $this->dataObjectHelper->populateWithArray(
  96. $dataObject,
  97. $data,
  98. \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class
  99. );
  100. return $dataObject;
  101. }
  102. }