PackageFactory.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Package;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Phrase;
  10. /**
  11. * Factory class for Package object
  12. *
  13. * @see Package
  14. */
  15. class PackageFactory
  16. {
  17. /**
  18. * Object Manager instance
  19. *
  20. * @var ObjectManagerInterface
  21. */
  22. private $objectManager;
  23. /**
  24. * PackageFactory constructor
  25. *
  26. * @param ObjectManagerInterface $objectManager
  27. * @param string $type
  28. */
  29. public function __construct(ObjectManagerInterface $objectManager, $type = Package::class)
  30. {
  31. $this->objectManager = $objectManager;
  32. $this->type = $type;
  33. }
  34. /**
  35. * Create new instance of Package object
  36. *
  37. * Since returned type can be configured via DI configuration, the method does check type of created object
  38. * and throws exception if that instance is not successor of \Magento\Deploy\Package
  39. *
  40. * @param array $arguments
  41. * @return Package
  42. * @throws LocalizedException
  43. */
  44. public function create(array $arguments)
  45. {
  46. $package = $this->objectManager->create($this->type, $arguments);
  47. if (!$package instanceof Package) {
  48. throw new LocalizedException(
  49. new Phrase("Wrong type specified: '%1'", [$this->type])
  50. );
  51. }
  52. return $package;
  53. }
  54. }