Skip to content

Commit

Permalink
Merge pull request #3397 from acrobat/refactor-fixtures
Browse files Browse the repository at this point in the history
[AllBundles] Refactor fixtures to dependency injection
  • Loading branch information
acrobat authored Apr 13, 2024
2 parents 8af13be + 1ecf961 commit 2551a68
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,35 @@

namespace {{ namespace }}\DataFixtures\ORM\LegalGenerator;

use {{ namespace }}\Entity\Pages\LegalFolderPage;
use {{ namespace }}\Entity\Pages\LegalPage;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Translatable\Entity\Repository\TranslationRepository;
use Gedmo\Translatable\Entity\Translation;
use Kunstmaan\AdminBundle\Entity\User;
use Kunstmaan\AdminBundle\Service\UserManager;
use Kunstmaan\CookieBundle\Entity\Cookie;
use Kunstmaan\CookieBundle\Entity\CookieType;
use Kunstmaan\MediaBundle\Entity\Folder;
use Kunstmaan\MediaBundle\Helper\Services\MediaCreatorService;
use Kunstmaan\NodeBundle\Entity\Node;
use Kunstmaan\AdminBundle\Entity\User;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Helper\Services\PageCreatorService;
use Kunstmaan\PagePartBundle\Helper\Services\PagePartCreatorService;
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use \Symfony\Component\HttpKernel\Kernel;
use Doctrine\ORM\EntityManagerInterface;
use Gedmo\Translatable\Entity\Repository\TranslationRepository;
use Gedmo\Translatable\Entity\Translation;
use {{ namespace }}\Entity\Pages\LegalFolderPage;
use {{ namespace }}\Entity\Pages\LegalPage;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Class DefaultFixtures
*/
class DefaultFixtures extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface, FixtureGroupInterface
class DefaultFixtures extends Fixture implements OrderedFixtureInterface, FixtureGroupInterface
{
// Username that is used for creating pages
const ADMIN_USERNAME = 'pagecreator';

/** @var ContainerInterface */
private $container;

/** @var ObjectManager */
private $manager;

Expand Down Expand Up @@ -67,6 +61,36 @@ class DefaultFixtures extends Fixture implements OrderedFixtureInterface, Contai
/** @var TranslationRepository */
private $translationRepo;

private string $projectDir;
private UserManager $userManager;
private string $secret;

public function __construct(
EntityManagerInterface $em,
PageCreatorService $pageCreator,
PagePartCreatorService $pagePartCreator,
MediaCreatorService $mediaCreator,
TranslatorInterface $translator,
SlugifierInterface $slugifier,
UserManager $userManager,
#[Autowire('%requiredlocales%')] string $requiredLocales,
#[Autowire('%kunstmaan_admin.default_locale%')] string $defaultLocale,
#[Autowire('%kernel.project_dir%')] string $projectDir,
#[Autowire('%kernel.secret%')] string $secret,
) {
$this->em = $em;
$this->pageCreator = $pageCreator;
$this->pagePartCreator = $pagePartCreator;
$this->mediaCreator = $mediaCreator;
$this->translator = $translator;
$this->slugifier = $slugifier;
$this->requiredLocales = explode('|', $requiredLocales);
$this->defaultLocale = $defaultLocale;
$this->projectDir = $projectDir;
$this->userManager = $userManager;
$this->secret = $secret;
}

/**
* Load data fixtures with the passed EntityManager.
*
Expand All @@ -76,14 +100,6 @@ public function load(ObjectManager $manager)
{
$this->manager = $manager;

$this->pageCreator = $this->container->get('kunstmaan_node.page_creator_service');
$this->mediaCreator = $this->container->get('kunstmaan_media.media_creator_service');
$this->pagePartCreator = $this->container->get('kunstmaan_pageparts.pagepart_creator_service');
$this->translator = $this->container->get('translator.default');
$this->slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$this->requiredLocales = explode('|', $this->container->getParameter('kunstmaan_admin.required_locales'));
$this->defaultLocale = $this->container->getParameter('kunstmaan_admin.default_locale');
$this->em = $this->container->get('doctrine.orm.entity_manager');
$this->translationRepo = $this->em->getRepository(Translation::class);

$this->checkPageCreator();
Expand Down Expand Up @@ -271,7 +287,7 @@ private function addCookiePreferencesPageParts(Node $node)
$pageparts = [];

$folder = $this->manager->getRepository(Folder::class)->findOneBy(['rel' => 'image']);
$imgDir = $this->container->getParameter('kernel.project_dir').'/assets/ui/img/legal/';
$imgDir = $this->projectDir.'/assets/ui/img/legal/';

$icon = $this->mediaCreator->createFile($imgDir.'cookie.svg', $folder->getId());
$pageparts['legal_header'][] = $this->pagePartCreator->getCreatorArgumentsForPagePartAndProperties(
Expand Down Expand Up @@ -468,14 +484,6 @@ public function getOrder()
return 52;
}

/**
* @param ContainerInterface|null $container
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* Check if we already have a page creator user, if not create one.
*/
Expand All @@ -488,9 +496,9 @@ private function checkPageCreator()
$user->setEmail(sprintf('%[email protected]', self::ADMIN_USERNAME));
$user->setUsername(self::ADMIN_USERNAME);
$user->setEnabled(1);
$user->setPlainPassword($this->container->getParameter('kernel.secret'));
$user->setPlainPassword($this->secret);

$this->container->get('kunstmaan_admin.user_manager')->updateUser($user);
$this->userManager->updateUser($user);

$user->setPasswordChanged(true);

Expand Down
29 changes: 10 additions & 19 deletions src/Kunstmaan/GeneratorBundle/DataFixtures/ORM/UserFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Kunstmaan\AdminBundle\Entity\User;
use Kunstmaan\AdminBundle\Service\UserManager;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Fixture for creating the admin and guest user
*/
class UserFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
class UserFixtures extends AbstractFixture implements OrderedFixtureInterface
{
const REFERENCE_ADMIN_USER = 'adminuser';

/** @var ContainerInterface */
private $container;
private UserManager $userManager;
private string $defaultAdminLocale;

/**
* Sets the Container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*
* @return void
*
* @api
*/
public function setContainer(?ContainerInterface $container = null)
public function __construct(UserManager $userManager, string $defaultAdminLocale)
{
$this->container = $container;
$this->userManager = $userManager;
$this->defaultAdminLocale = $defaultAdminLocale;
}

/**
Expand All @@ -46,7 +37,7 @@ public function load(ObjectManager $manager)
'admin',
$password,
'[email protected]',
$this->container->getParameter('kunstmaan_admin.default_admin_locale'),
$this->defaultAdminLocale,
['ROLE_SUPER_ADMIN'],
[$manager->merge($this->getReference(GroupFixtures::REFERENCE_SUPERADMINS_GROUP))],
true,
Expand Down Expand Up @@ -89,7 +80,7 @@ private function createUser(
$enabled = false,
$changed = false
) {
$user = $this->container->get('kunstmaan_admin.user_manager')->createUser();
$user = $this->userManager->createUser();
$user->setUsername($username);
$user->setPlainPassword($password);
$user->setRoles($roles);
Expand All @@ -101,7 +92,7 @@ private function createUser(
$user->addGroup($group);
}

$this->container->get('kunstmaan_admin.user_manager')->updateUser($user);
$this->userManager->updateUser($user);

$user->setPasswordChanged($changed);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,73 @@
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;
use Faker\Provider\DateTime;
use Faker\Provider\Lorem;
use Kunstmaan\NodeBundle\Entity\Node;
use Kunstmaan\NodeBundle\Helper\Services\PageCreatorService;
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Kunstmaan\PagePartBundle\Helper\Services\PagePartCreatorService;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use {{ namespace }}\Entity\Pages\{{ entity_class }}OverviewPage;
use {{ namespace }}\Entity\Pages\{{ entity_class }}Page;
use {{ namespace }}\Entity\{{ entity_class }}Author;

/**
* {{ entity_class }}ArticleFixtures
*/
class {{ entity_class }}ArticleFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface, ORMFixtureInterface
class {{ entity_class }}ArticleFixtures extends AbstractFixture implements OrderedFixtureInterface, ORMFixtureInterface
{
/**
* @var ContainerInterface
*/
private $container = null;
private EntityManagerInterface $em;
private PageCreatorService $pageCreator;
private PagePartCreatorService $pagePartCreator;
private SlugifierInterface $slugifier;
private bool $isMultiLanguage;
private string $requiredLocales;

public function __construct(
EntityManagerInterface $em,
PageCreatorService $pageCreator,
PagePartCreatorService $pagePartCreator,
SlugifierInterface $slugifier,
#[Autowire('%kunstmaan_admin.multi_language%')] bool $isMultiLanguage,
#[Autowire('%kunstmaan_admin.required_locales%')] string $requiredLocales,
) {
$this->em = $em;
$this->pageCreator = $pageCreator;
$this->pagePartCreator = $pagePartCreator;
$this->slugifier = $slugifier;
$this->isMultiLanguage = $isMultiLanguage;
$this->requiredLocales = $requiredLocales;
}

/**
* Load data fixtures with the passed EntityManager.
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
if ($this->container->getParameter('kunstmaan_admin.multi_language')) {
$languages = explode('|', $this->container->getParameter('kunstmaan_admin.required_locales'));
}
if (!is_array($languages) || count($languages) < 1) {
$languages = array('en');
}

$em = $this->container->get('doctrine.orm.entity_manager');

$pageCreator = $this->container->get('kunstmaan_node.page_creator_service');
$ppCreatorService = $this->container->get('kunstmaan_pageparts.pagepart_creator_service');
{
if ($this->isMultiLanguage) {
$languages = explode('|', $this->requiredLocales);
}
if (!is_array($languages) || count($languages) < 1) {
$languages = array('en');
}

// Create article overview page
$nodeRepo = $em->getRepository(Node::class);
$homePage = $nodeRepo->findOneBy(array('internalName' => 'homepage'));
// Create article overview page
$nodeRepo = $this->em->getRepository(Node::class);
$homePage = $nodeRepo->findOneBy(array('internalName' => 'homepage'));

$overviewPage = new {{ entity_class }}OverviewPage();
$overviewPage->setTitle('{{ entity_class }}');
$overviewPage = new {{ entity_class }}OverviewPage();
$overviewPage->setTitle('{{ entity_class }}');

$translations = array();
foreach ($languages as $lang) {
$title = '{{ entity_class }}';
$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title) {
$translation->setTitle($title);
$translation->setWeight(30);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
$translation->setSlug($this->slugifier->slugify($title));
});
}

Expand All @@ -72,27 +82,26 @@ public function load(ObjectManager $manager)
'creator' => 'admin'
);

$pageCreator->createPage($overviewPage, $translations, $options);
$this->pageCreator->createPage($overviewPage, $translations, $options);

$fakerNL = Factory::create('nl_BE');
$fakerEN = Factory::create('en_US');
$fakerNL = Factory::create('nl_BE');
$fakerEN = Factory::create('en_US');

// Create articles
for ($i=1; $i<=6; $i++) {

{% if uses_author %}
// Create author
$author = new {{ entity_class }}Author();
$author->setName($fakerNL->name);
$manager->persist($author);
$manager->flush();
{% endif %}
for ($i=1; $i<=6; $i++) {
{% if uses_author %}
// Create author
$author = new {{ entity_class }}Author();
$author->setName($fakerNL->name);
$manager->persist($author);
$manager->flush();
{% endif %}

$articlePage = new {{ entity_class }}Page();
$articlePage->setTitle(Lorem::sentence(6));
{% if uses_author %}
$articlePage->setTitle(Lorem::sentence(6));
{% if uses_author %}
$articlePage->setAuthor($author);
{% endif %}
{% endif %}
$articlePage->setDate(DateTime::dateTimeBetween('-'.($i+1).' days', '-'.$i.' days'));
$articlePage->setSummary(Lorem::paragraph(5));

Expand All @@ -107,8 +116,7 @@ public function load(ObjectManager $manager)
$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title, $i) {
$translation->setTitle($title);
$translation->setWeight(100 + $i);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
$translation->setSlug($this->slugifier->slugify($title));
});
}

Expand All @@ -119,29 +127,24 @@ public function load(ObjectManager $manager)
'creator' => 'admin'
);

$articlePage = $pageCreator->createPage($articlePage, $translations, $options);
$articlePage = $this->pageCreator->createPage($articlePage, $translations, $options);

foreach ($languages as $lang) {
$pageparts = array(
'main' => array(
$ppCreatorService->getCreatorArgumentsForPagePartAndProperties('{{ namespace }}\Entity\PageParts\TextPagePart',
$this->pagePartCreator->getCreatorArgumentsForPagePartAndProperties('{{ namespace }}\Entity\PageParts\TextPagePart',
array('setContent' => '<p>'.Lorem::paragraph(15).'</p>' . '<p>'.Lorem::paragraph(25).'</p>' .'<p>'.Lorem::paragraph(10).'</p>')
)
)
);

$ppCreatorService->addPagePartsToPage($articlePage, $pageparts, $lang);
$this->pagePartCreator->addPagePartsToPage($articlePage, $pageparts, $lang);
}
}
}

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

public function setContainer(ContainerInterface $container = null): void
{
$this->container = $container;
}
{
return 60;
}
}
Loading

0 comments on commit 2551a68

Please sign in to comment.