Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Improve fixtures 2
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk authored and jordisala1991 committed Jul 18, 2020
1 parent 97bf3bd commit 0adb96c
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 502 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parameters:
sonata_media.cdn.host: '/uploads/media/'
sonata_user.google_authenticator.server: demo.sonata-project.org
sonata_page.varnish.command: 'if [ ! -r "/etc/varnish/secret" ]; then echo "VALID ERROR :/"; else varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 {{ COMMAND }} "{{ EXPRESSION }}"; fi;'
sonata.fixtures.product.fake: 0
sonata.fixtures.product.fake: 10
sonata.fixtures.customer.fake: 10
sonata.fixtures.page.create_subsite: false

Expand Down
1 change: 1 addition & 0 deletions src/AppBundle/Entity/Classification/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace AppBundle\Entity\Classification;

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\ClassificationBundle\Entity\BaseCollection;

/**
Expand Down
211 changes: 57 additions & 154 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadCategoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Sonata\ClassificationBundle\Model\CategoryManagerInterface;
use Sonata\ClassificationBundle\Entity\CategoryManager;
use Sonata\ClassificationBundle\Model\Category;
use Sonata\ClassificationBundle\Model\Context;

/**
* Category fixtures loader.
Expand All @@ -26,228 +28,129 @@
class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* @var CategoryManagerInterface
* @var CategoryManager
*/
protected $categoryManager;

public function __construct(CategoryManagerInterface $categoryManager)
public function __construct(CategoryManager $categoryManager)
{
$this->categoryManager = $categoryManager;
}

public function getOrder()
public function getOrder(): int
{
return 2;
}

public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
$defaultContext = $this->getReference('context_default');

$defaultCategory = $this->categoryManager->create();
$defaultCategory->setName('Default Category');
$defaultCategory->setSlug('products');
$defaultCategory->setEnabled(true);
$defaultCategory->setContext($defaultContext);

// Default category
$defaultCategory = $this->createRootCategory('Default Category', 'default', $this->getReference('context_default'));
$this->setReference('root_default_category', $defaultCategory);

$this->categoryManager->save($defaultCategory);

$productContext = $this->getReference('context_product_catalog');

$rootProduct = $this->categoryManager->create();
$rootProduct->setName('Root Products');
$rootProduct->setSlug('products');
$rootProduct->setEnabled(true);
$rootProduct->setContext($productContext);
$this->setReference('products_category', $rootProduct);

// Root product category
$rootProduct = $this->createRootCategory('Root Products', 'products', $this->getReference('context_product_catalog'));
$this->setReference('root_products_category', $rootProduct);

$this->categoryManager->save($rootProduct);

// Travels category
$travels = $this->categoryManager->create();
$travels->setName('Travels');
$travels->setSlug('travels');
$travels->setParent($rootProduct);
$travels->setDescription('Discover our travels');
$travels->setEnabled(true);
$this->categoryManager->save($travels);
$travels = $this->createCategory('Travels', 'travels', 'Discover our travels', true, $rootProduct);
$this->setReference('travels_category', $travels);

// Asia category
$asia = $this->categoryManager->create();
$asia->setParent($travels);
$asia->setName('Asia');
$asia->setSlug('asia');
$asia->setDescription('Want to travel in Asia? Check out our travels.');
$asia->setEnabled(true);
$this->categoryManager->save($asia);
$asia = $this->createCategory('Asia', 'asia', 'Want to travel in Asia? Check out our travels.', true, $travels);
$this->setReference('travels_asia_category', $asia);

// Japan category
$japan = $this->categoryManager->create();
$japan->setParent($asia);
$japan->setName('Japan');
$japan->setSlug('japan');
$japan->setDescription('Want to travel in Japan? Check out our travels.');
$japan->setEnabled(true);
$this->categoryManager->save($japan);
$japan = $this->createCategory('Japan', 'japan', 'Want to travel in Japan? Check out our travels.', true, $asia);
$this->setReference('travels_japan_category', $japan);

// North America category
$northAmerica = $this->categoryManager->create();
$northAmerica->setParent($travels);
$northAmerica->setName('North America');
$northAmerica->setSlug('north-america');
$northAmerica->setDescription('Want to travel in North America? Check out our travels.');
$northAmerica->setEnabled(true);
$this->categoryManager->save($northAmerica);
$northAmerica = $this->createCategory('North America', 'north-america', 'Want to travel in North America? Check out our travels.', true, $travels);
$this->setReference('travels_north_america_category', $northAmerica);

// Canada category
$canada = $this->categoryManager->create();
$canada->setParent($northAmerica);
$canada->setName('Canada');
$canada->setSlug('canada');
$canada->setDescription('Want to travel in Canada? Check out our travels.');
$canada->setEnabled(true);
$this->categoryManager->save($canada);
$canada = $this->createCategory('Canada', 'canada', 'Want to travel in Canada? Check out our travels.', true, $northAmerica);
$this->setReference('travels_canada_category', $canada);

// Quebec category
$quebec = $this->categoryManager->create();
$quebec->setParent($canada);
$quebec->setName('Quebec');
$quebec->setSlug('quebec');
$quebec->setDescription('Want to travel in Quebec? Check out our travels.');
$quebec->setEnabled(true);
$this->categoryManager->save($quebec);
$quebec = $this->createCategory('Quebec', 'quebec', 'Want to travel in Quebec? Check out our travels.', true, $canada);
$this->setReference('travels_quebec_category', $quebec);

// Europe category
$europe = $this->categoryManager->create();
$europe->setParent($travels);
$europe->setName('Europe');
$europe->setSlug('europe');
$europe->setDescription('Want to travel in Europe? Check out our travels.');
$europe->setEnabled(true);
$this->categoryManager->save($europe);
$europe = $this->createCategory('Europe', 'europe', 'Want to travel in Europe? Check out our travels.', true, $travels);
$this->setReference('travels_europe_category', $europe);

// Switzerland category
$switzerland = $this->categoryManager->create();
$switzerland->setParent($europe);
$switzerland->setName('Switzerland');
$switzerland->setSlug('switzerland');
$switzerland->setDescription('Want to travel in Switzerland? Check out our travels.');
$switzerland->setEnabled(true);
$this->categoryManager->save($switzerland);
$switzerland = $this->createCategory('Switzerland', 'switzerland', 'Want to travel in Switzerland? Check out our travels.', true, $europe);
$this->setReference('travels_switzerland_category', $switzerland);

// France category
$france = $this->categoryManager->create();
$france->setParent($europe);
$france->setName('France');
$france->setSlug('france');
$france->setDescription('Want to travel in France? Check out our travels.');
$france->setEnabled(true);
$this->categoryManager->save($france);
$france = $this->createCategory('France', 'france', 'Want to travel in France? Check out our travels.', true, $europe);
$this->setReference('travels_france_category', $france);

// Paris category
$paris = $this->categoryManager->create();
$paris->setParent($france);
$paris->setName('Paris');
$paris->setSlug('paris');
$paris->setDescription('Want to travel in Paris? Check out our travels.');
$paris->setEnabled(true);
$this->categoryManager->save($paris);
$paris = $this->createCategory('Paris', 'paris', 'Want to travel in Paris? Check out our travels.', true, $europe);
$this->setReference('travels_paris_category', $paris);

// Great britain category
$greatBritain = $this->categoryManager->create();
$greatBritain->setParent($travels);
$greatBritain->setName('Great Britain');
$greatBritain->setSlug('great-britain');
$greatBritain->setDescription('Want to travel in Great Britain? Check out our travels.');
$greatBritain->setEnabled(true);
$this->categoryManager->save($greatBritain);
$greatBritain = $this->createCategory('Great Britain', 'great-britain', 'Want to travel in Great Britain? Check out our travels.', true, $travels);
$this->setReference('travels_great_britain_category', $greatBritain);

// London category
$london = $this->categoryManager->create();
$london->setParent($travels);
$london->setName('London');
$london->setSlug('london');
$london->setDescription('Want to travel in London? Check out our travels.');
$london->setEnabled(true);
$this->categoryManager->save($london);
$london = $this->createCategory('London', 'london', 'Want to travel in London? Check out our travels.', true, $travels);
$this->setReference('travels_london_category', $london);

// Goodies category
$goodies = $this->categoryManager->create();
$goodies->setName('Goodies');
$goodies->setSlug('goodies');
$goodies->setDescription('Some goodies related to Sonata and Symfony world.');
$goodies->setEnabled(true);
$goodies->setParent($rootProduct);
$this->categoryManager->save($goodies);
$goodies = $this->createCategory('Goodies', 'goodies', 'Some goodies related to Sonata and Symfony world.', true, $rootProduct);
$this->setReference('goodies_category', $goodies);

// Goodies sub-categories
$plushes = $this->categoryManager->create();
$plushes->setParent($goodies);
$plushes->setName('Plushes');
$plushes->setSlug('plushes');
$plushes->setDescription('Some plushes.');
$plushes->setEnabled(true);
$this->categoryManager->save($plushes);
$plushes = $this->createCategory('Plushes', 'plushes', 'Some plushes.', true, $goodies);
$this->setReference('plushes_goodies_category', $plushes);

// Mugs sub-categories
$mugs = $this->categoryManager->create();
$mugs->setParent($goodies);
$mugs->setName('Mugs');
$mugs->setSlug('mugs');
$mugs->setDescription('Some mugs.');
$mugs->setEnabled(true);
$this->categoryManager->save($mugs);
$mugs = $this->createCategory('Mugs', 'mugs', 'Some mugs.', true, $goodies);
$this->setReference('sonata_mugs_category', $mugs);

// Clothing sub-categories
$clothes = $this->categoryManager->create();
$clothes->setParent($goodies);
$clothes->setName('Clothes');
$clothes->setSlug('clothes');
$clothes->setDescription('Clothes for geeks.');
$clothes->setEnabled(true);
$this->categoryManager->save($clothes);
$clothes = $this->createCategory('Clothes', 'clothes', 'Clothes for geeks.', true, $goodies);
$this->setReference('sonata_clothes_category', $clothes);

// Dummy category
$dummy = $this->categoryManager->create();
$dummy->setName('Dummy');
$dummy->setSlug('dummy');
$dummy->setDescription('Dummy category with many dummy products');
$dummy->setEnabled(true);
$dummy->setParent($rootProduct);
$this->categoryManager->save($dummy);

$dummy = $this->createCategory('Dummy', 'dummy', 'Dummy category with many dummy products.', true, $rootProduct);
$this->setReference('dummy_category', $dummy);

// Disabled category for testing purpose
$shoes = $this->categoryManager->create();
$shoes->setParent($goodies);
$shoes->setName('Shoes');
$shoes->setSlug('shoes');
$shoes->setDescription('Get the last coolest Sonata shoes (seriously)');
$shoes->setEnabled(true);
$this->categoryManager->save($shoes);
$shoes = $this->createCategory('Shoes', 'shoes', 'Get the last coolest Sonata shoes (seriously)', false, $goodies);
$this->setReference('sonata_shoes_category', $shoes);

$manager->flush();
$this->categoryManager->getObjectManager()->flush();
}

protected function createRootCategory(string $name, string $slug, Context $context): Category
{
$rootCategory = $this->categoryManager->create();
$rootCategory->setName($name);
$rootCategory->setSlug($slug);
$rootCategory->setEnabled(true);
$rootCategory->setContext($context);

$this->categoryManager->save($rootCategory, false);

return $rootCategory;
}

protected function createCategory(string $name, string $slug, string $description, bool $enabled, Category $parent): Category
{
$category = $this->categoryManager->create();
$category->setParent($parent);
$category->setName($name);
$category->setSlug($slug);
$category->setDescription($description);
$category->setEnabled($enabled);

$this->categoryManager->save($category, false);

return $category;
}
}
Loading

0 comments on commit 0adb96c

Please sign in to comment.