Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Tests\DataFixtures;
- use Doctrine\Bundle\FixturesBundle\Fixture;
- use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
- use Doctrine\Common\DataFixtures\DependentFixtureInterface;
- use Doctrine\Common\Persistence\ObjectManager;
- use Ramsey\Uuid\Uuid;
- use SDK\Bundle\ORMClientBundle\DoctrineORM\ObjectManagerFactory;
- use SDK\Bundle\ORMClientBundle\DoctrineORM\ReferenceRepository;
- use SharedModel\Entity\PIM\Attribute;
- use SharedModel\Entity\PIM\AttributeOption;
- use SharedModel\Entity\PIM\Item;
- use SharedModel\Entity\PIM\ItemAttributeValue;
- use SharedModel\ValueObject\Entity\PIM\AttributeType;
- class ItemAttributeValueFixtures extends Fixture implements FixtureGroupInterface, DependentFixtureInterface
- {
- public const COLOUR_ATTRIBUTE_VALUES = ['black', 'red', 'green'];
- /**
- * @var ObjectManagerFactory
- */
- protected $objectManagerFactory;
- /**
- * @var ReferenceRepository
- */
- protected $customReferenceRepository;
- /**
- * @param ObjectManagerFactory $objectManagerFactory
- * @param ReferenceRepository $referenceRepository
- */
- public function __construct(ObjectManagerFactory $objectManagerFactory, ReferenceRepository $referenceRepository)
- {
- $this->objectManagerFactory = $objectManagerFactory;
- $this->customReferenceRepository = $referenceRepository;
- }
- /**
- * {@inheritdoc}
- */
- public function getDependencies()
- {
- return [
- BrandFixtures::class,
- OrganizationFixtures::class,
- ManufacturerFixtures::class,
- ItemFixtures::class,
- ];
- }
- /**
- * {@inheritdoc}
- */
- public static function getGroups(): array
- {
- return ['test'];
- }
- /**
- * @param ObjectManager $manager
- *
- * @throws \Exception
- */
- public function load(ObjectManager $manager)
- {
- $this->setReferenceRepository($this->customReferenceRepository);
- $itemAttributeValueManager = $this->objectManagerFactory->getManager(ItemAttributeValue::class);
- $attributeManager = $this->objectManagerFactory->getManager(Attribute::class);
- $attributeOptionManager = $this->objectManagerFactory->getManager(AttributeOption::class);
- $attribute = $this->generateAttribute(new AttributeType(AttributeType::TYPE_TEXT));
- $attributeOption = $this->generateAttributeOption($attribute);
- $attributeOptionManager->persist($attributeOption);
- $attributeManager->persist($attribute);
- foreach ($this->getItems() as $key => $item) {
- $itemAttributeValue = $this->generateItemAttributeValue($item, $attribute, $key);
- $itemAttributeValueManager->persist($itemAttributeValue);
- }
- }
- /**
- * @param Item $item
- * @param Attribute $attribute
- * @param int $iteration
- *
- * @return ItemAttributeValue
- * @throws \Exception
- */
- private function generateItemAttributeValue(Item $item, Attribute $attribute, int $iteration): ItemAttributeValue
- {
- switch ($iteration) {
- case 0 === $iteration % 2:
- $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[0];
- break;
- case 0 === $iteration % 3:
- $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[1];
- break;
- default:
- $attributeColour = self::COLOUR_ATTRIBUTE_VALUES[2];
- }
- return (new ItemAttributeValue(Uuid::uuid4()))
- ->setItem($item)
- ->setAttribute($attribute)
- ->setValue($attributeColour);
- }
- private function getItems(): \Generator
- {
- $itemsList = $this->objectManagerFactory->getRepository(Item::class)->findAll();
- foreach ($itemsList as $item) {
- yield $item;
- }
- }
- private function generateAttributeOption(Attribute $attribute): AttributeOption
- {
- return (new AttributeOption(Uuid::uuid4()))
- ->setSortOrder(1)
- ->setValue('Wood')
- ->setAttribute($attribute);
- }
- private function generateAttribute(AttributeType $attributeType): Attribute
- {
- return (new Attribute(Uuid::fromString(Uuid::uuid4())))
- ->setLabel('Colour')
- ->setCode('colour')
- ->setDefaultValue('')
- ->setClass('')
- ->setFrontend("1")
- ->setIsUnique(1)
- ->setType($attributeType);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement