Advertisement
evorclass

Untitled

May 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Tests\DataFixtures;
  4.  
  5. use Doctrine\Bundle\FixturesBundle\Fixture;
  6. use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
  7. use Doctrine\Common\DataFixtures\DependentFixtureInterface;
  8. use Doctrine\Common\Persistence\ObjectManager;
  9. use Ramsey\Uuid\Uuid;
  10. use SDK\Bundle\ORMClientBundle\DoctrineORM\ObjectManagerFactory;
  11. use SDK\Bundle\ORMClientBundle\DoctrineORM\ReferenceRepository;
  12. use SharedModel\Entity\PIM\Attribute;
  13. use SharedModel\Entity\PIM\AttributeOption;
  14. use SharedModel\Entity\PIM\Item;
  15. use SharedModel\Entity\PIM\ItemAttributeValue;
  16. use SharedModel\ValueObject\Entity\PIM\AttributeType;
  17.  
  18. class ItemAttributeValueFixtures extends Fixture implements FixtureGroupInterface, DependentFixtureInterface
  19. {
  20. public const COLOUR_ATTRIBUTE_VALUES = ['black', 'red', 'green'];
  21.  
  22. /**
  23. * @var ObjectManagerFactory
  24. */
  25. protected $objectManagerFactory;
  26.  
  27. /**
  28. * @var ReferenceRepository
  29. */
  30. protected $customReferenceRepository;
  31.  
  32. /**
  33. * @param ObjectManagerFactory $objectManagerFactory
  34. * @param ReferenceRepository $referenceRepository
  35. */
  36. public function __construct(ObjectManagerFactory $objectManagerFactory, ReferenceRepository $referenceRepository)
  37. {
  38. $this->objectManagerFactory = $objectManagerFactory;
  39. $this->customReferenceRepository = $referenceRepository;
  40. }
  41.  
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getDependencies()
  46. {
  47. return [
  48. BrandFixtures::class,
  49. OrganizationFixtures::class,
  50. ManufacturerFixtures::class,
  51. ItemFixtures::class,
  52. ];
  53. }
  54.  
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function getGroups(): array
  59. {
  60. return ['test'];
  61. }
  62.  
  63. /**
  64. * @param ObjectManager $manager
  65. *
  66. * @throws \Exception
  67. */
  68. public function load(ObjectManager $manager)
  69. {
  70. $this->setReferenceRepository($this->customReferenceRepository);
  71. $itemAttributeValueManager = $this->objectManagerFactory->getManager(ItemAttributeValue::class);
  72. $attributeManager = $this->objectManagerFactory->getManager(Attribute::class);
  73. $attributeOptionManager = $this->objectManagerFactory->getManager(AttributeOption::class);
  74.  
  75. $attribute = $this->generateAttribute(new AttributeType(AttributeType::TYPE_TEXT));
  76. $attributeOption = $this->generateAttributeOption($attribute);
  77.  
  78. $attributeOptionManager->persist($attributeOption);
  79. $attributeManager->persist($attribute);
  80.  
  81. foreach ($this->getItems() as $key => $item) {
  82. $itemAttributeValue = $this->generateItemAttributeValue($item, $attribute, $key);
  83. $itemAttributeValueManager->persist($itemAttributeValue);
  84. }
  85. }
  86.  
  87. /**
  88. * @param Item $item
  89. * @param Attribute $attribute
  90. * @param int $iteration
  91. *
  92. * @return ItemAttributeValue
  93. * @throws \Exception
  94. */
  95. private function generateItemAttributeValue(Item $item, Attribute $attribute, int $iteration): ItemAttributeValue
  96. {
  97. switch ($iteration) {
  98. case 0 === $iteration % 2:
  99. $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[0];
  100. break;
  101. case 0 === $iteration % 3:
  102. $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[1];
  103. break;
  104. default:
  105. $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[2];
  106. }
  107.  
  108. return (new ItemAttributeValue(Uuid::uuid4()))
  109. ->setItem($item)
  110. ->setAttribute($attribute)
  111. ->setValue($attributeColour);
  112. }
  113.  
  114. private function getItems(): \Generator
  115. {
  116. $itemsList = $this->objectManagerFactory->getRepository(Item::class)->findAll();
  117. foreach ($itemsList as $item) {
  118. yield $item;
  119. }
  120. }
  121.  
  122. private function generateAttributeOption(Attribute $attribute): AttributeOption
  123. {
  124. return (new AttributeOption(Uuid::uuid4()))
  125. ->setSortOrder(1)
  126. ->setValue('Wood')
  127. ->setAttribute($attribute);
  128. }
  129.  
  130. private function generateAttribute(AttributeType $attributeType): Attribute
  131. {
  132. return (new Attribute(Uuid::fromString(Uuid::uuid4())))
  133. ->setLabel('Colour')
  134. ->setCode('colour')
  135. ->setDefaultValue('')
  136. ->setClass('')
  137. ->setFrontend("1")
  138. ->setIsUnique(1)
  139. ->setType($attributeType);
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement