mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
123 lines
3.7 KiB
PHP
123 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Chill\ThirdPartyBundle\DataFixtures\ORM;
|
|
|
|
use Chill\MainBundle\DataFixtures\ORM\LoadCenters;
|
|
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
|
|
use Chill\MainBundle\Entity\PostalCode;
|
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
|
use Doctrine\Persistence\ObjectManager;
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
|
use Nelmio\Alice\Loader\NativeLoader;
|
|
use Nelmio\Alice\ObjectSet;
|
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
|
use Chill\MainBundle\Entity\Address;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
class LoadThirdParty extends Fixture Implements DependentFixtureInterface
|
|
{
|
|
public function load(ObjectManager $manager)
|
|
{
|
|
$thirdParties = $this->getThirdParties()->getObjects();
|
|
|
|
foreach ($thirdParties as $name => $thirdParty) {
|
|
if ('a' === $name[0]) {
|
|
// this is an address
|
|
continue;
|
|
}
|
|
|
|
foreach ($this->getCenters() as $center) {
|
|
$thirdParty->addCenter($center);
|
|
}
|
|
|
|
$manager->persist($thirdParty);
|
|
}
|
|
|
|
$manager->flush();
|
|
}
|
|
|
|
private function getCenters(): \Iterator
|
|
{
|
|
$references = \array_map(function($a) { return $a['ref']; },
|
|
LoadCenters::$centers);
|
|
$number = random_int(1, count($references));
|
|
|
|
if ($number === 1) {
|
|
yield $this->getReference($references[\array_rand($references)]);
|
|
} else {
|
|
foreach (array_rand($references, $number) as $index) {
|
|
yield $this->getReference($references[$index]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getDependencies()
|
|
{
|
|
return [
|
|
LoadCenters::class,
|
|
LoadPostalCodes::class
|
|
];
|
|
}
|
|
|
|
private function getThirdParties(): ObjectSet
|
|
{
|
|
$loader = new NativeLoader();
|
|
$objectSet = $loader->loadData([
|
|
Address::class => [
|
|
'address{1..75}' => [
|
|
'street' => '<fr_FR:streetName()>',
|
|
'streetNumber' => '<fr_FR:buildingNumber()>',
|
|
'validFrom' => '<dateTimeBetween(\'-1 year\', \'now\')>',
|
|
'postCode' => $this->getPostalCode()
|
|
],
|
|
],
|
|
ThirdParty::class => [
|
|
'thirdparty{1..75}' => [
|
|
'name' => '<fr_FR:company()>',
|
|
'telephone' => '<fr_FR:phonenumber()>',
|
|
'email' => '<email()>',
|
|
'comment' => '<fr_FR:realTextBetween(10, 500)>',
|
|
'address' => '@address<current()>'
|
|
]
|
|
]
|
|
]);
|
|
|
|
return $objectSet;
|
|
}
|
|
|
|
private function getPostalCode(): PostalCode
|
|
{
|
|
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
|
return $this->getReference($ref);
|
|
if (count($this->postalCodesIds) === 0) {
|
|
// fill the postal codes
|
|
$this->em->createQuery('SELECT p.id FROM '.PostalCode::class)
|
|
->getScalarResult();
|
|
}
|
|
|
|
$id = $this->postalCodesIds[\array_rand($this->postalCodesIds)];
|
|
|
|
return $this->em->getRepository(PostalCode::class)
|
|
->find($id);
|
|
}
|
|
|
|
private function createAddress(): ObjectSet
|
|
{
|
|
$loader = new NativeLoader();
|
|
$objectSet = $loader->loadData([
|
|
Address::class => [
|
|
'address1' => [
|
|
'name' => '<fr_FR:company()>',
|
|
'telephone' => '<fr_FR:phonenumber()>',
|
|
'email' => '<email()>',
|
|
'comment' => '<fr_FR:realTextBetween(10, 500)>'
|
|
]
|
|
]
|
|
]);
|
|
|
|
return $objectSet;
|
|
|
|
}
|
|
|
|
}
|