-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3397 from acrobat/refactor-fixtures
[AllBundles] Refactor fixtures to dependency injection
- Loading branch information
Showing
8 changed files
with
195 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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. | ||
* | ||
|
@@ -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(); | ||
|
@@ -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( | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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, | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.