Factory.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Backup object factory.
  8. */
  9. namespace Magento\Framework\Backup;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\ObjectManagerInterface;
  12. use Magento\Framework\Phrase;
  13. /**
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Factory
  18. {
  19. /**
  20. * Object manager
  21. *
  22. * @var ObjectManagerInterface
  23. */
  24. private $_objectManager;
  25. /**
  26. * Backup type constant for database backup
  27. */
  28. const TYPE_DB = 'db';
  29. /**
  30. * Backup type constant for filesystem backup
  31. */
  32. const TYPE_FILESYSTEM = 'filesystem';
  33. /**
  34. * Backup type constant for full system backup(database + filesystem)
  35. */
  36. const TYPE_SYSTEM_SNAPSHOT = 'snapshot';
  37. /**
  38. * Backup type constant for media and database backup
  39. */
  40. const TYPE_MEDIA = 'media';
  41. /**
  42. * Backup type constant for full system backup excluding media folder
  43. */
  44. const TYPE_SNAPSHOT_WITHOUT_MEDIA = 'nomedia';
  45. /**
  46. * List of supported a backup types
  47. *
  48. * @var string[]
  49. */
  50. protected $_allowedTypes;
  51. /**
  52. * @param ObjectManagerInterface $objectManager
  53. */
  54. public function __construct(ObjectManagerInterface $objectManager)
  55. {
  56. $this->_objectManager = $objectManager;
  57. $this->_allowedTypes = [
  58. self::TYPE_DB,
  59. self::TYPE_FILESYSTEM,
  60. self::TYPE_SYSTEM_SNAPSHOT,
  61. self::TYPE_MEDIA,
  62. self::TYPE_SNAPSHOT_WITHOUT_MEDIA,
  63. ];
  64. }
  65. /**
  66. * Create new backup instance
  67. *
  68. * @param string $type
  69. * @return BackupInterface
  70. * @throws LocalizedException
  71. */
  72. public function create($type)
  73. {
  74. if (!in_array($type, $this->_allowedTypes)) {
  75. throw new LocalizedException(
  76. new Phrase(
  77. 'Current implementation not supported this type (%1) of backup.',
  78. [$type]
  79. )
  80. );
  81. }
  82. $class = 'Magento\Framework\Backup\\' . ucfirst($type);
  83. return $this->_objectManager->create($class);
  84. }
  85. }