Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\DataFixtures;
- use App\Entity\BackLink;
- use App\Entity\Company;
- use App\Entity\User;
- use App\Entity\Website;
- use DateTime;
- use Doctrine\Bundle\FixturesBundle\Fixture;
- use Doctrine\Persistence\ObjectManager;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- class BackLinkFixtures extends Fixture
- {
- private $companyList = [];
- private $websiteList = [];
- private $userList = [];
- public function loadFixtures(): void
- {
- // creates list of companies
- $this->createCompanies();
- // creates lists of users
- $this->createUsers();
- // creates lists of websites
- $this->createWebsites();
- for ($i = 0; $i < 10; $i++) {
- // this loop creates 10 back links for for each loop above
- for ($id = 1; $id <= 10; $id++) {
- $this->createBackLink(
- true,
- 'Technology' . $id,
- 'www.technology.com' . $id,
- 'Technology Comment' . $id,
- $this->websiteList[$i],
- $this->userList[$i]
- );
- }
- }
- }
- private function createBackLink(bool $pulse, string $keyword, string $link, string $comment, int $websiteID, int $userID): void {
- $backLink = new BackLink();
- $backLink->setPulse($pulse);
- $backLink->setCreatedBy($userID);
- $backLink->setComment($comment);
- $backLink->setKeyword($keyword);
- $backLink->setLink($link);
- $backLink->setWebsite($websiteID);
- $this->em->persist($backLink);
- $this->em->flush();
- }
- private function createCompanies(): void
- {
- $companies = [
- $google = [
- "name" => "Google",
- "siret" => "Siret",
- "AddressLine1" => "USA",
- "AddressLine2" => "Texas",
- "PostalCode" => "21290",
- "city" => "Texas",
- "country" => "USA",
- "phone" => "+13546389",
- "email" => "google@google.com"
- ],
- $amazon = [
- "name" => "Amazon",
- "siret" => "Siret",
- "AddressLine1" => "USA",
- "AddressLine2" => "Texas",
- "PostalCode" => "21290",
- "city" => "Texas",
- "country" => "USA",
- "phone" => "+13546389",
- "email" => "amazon@amazon.com"
- ],
- $facebook = [
- "name" => "Facebook",
- "siret" => "Siret",
- "AddressLine1" => "USA",
- "AddressLine2" => "Texas",
- "PostalCode" => "21290",
- "city" => "Texas",
- "country" => "USA",
- "phone" => "+13546389",
- "email" => "facebook@facebook.com"
- ]];
- for ($i = 0; $i < 3; $i++) {
- array_push($this->companyList, $this->newCompany($companies[$i]));
- }
- }
- private function createWebsites(): void
- {
- $urls = [
- 'google.com',
- 'facebook.com',
- 'amazon.com',
- 'twitter.com',
- 'google.com',
- 'facebook.com',
- 'amazon.com',
- 'twitter.com',
- 'google.com',
- 'facebook.com'
- ];
- for ($i = 0; $i < 10; $i++) {
- array_push($this->websiteList, $this->newWebsite($this->companyList[mt_rand(0, count($this->companyList) - 1)], $urls[$i]));
- }
- }
- private function createUsers(): void
- {
- for ($i = 0; $i < 2; $i++) {
- $companyArrayIndex = [0, 2];
- array_push($this->userList, $this->newUser($this->companyList[$companyArrayIndex[$i]]));
- }
- }
- private function newWebsite(int $companyID, string $url): int
- {
- $website = new Website();
- $website->setCompany($companyID)
- ->setUrl($url);
- $this->em->persist($website);
- $this->em->flush();
- return $website->getID();
- }
- private function newCompany(Company $companyValues): int
- {
- $company = new Company();
- $company->setName($companyValues['name'])
- ->setSiret($companyValues['siret'])
- ->setAddressLine1($companyValues['AddressLine1'])
- ->setAddressLine2($companyValues['AddressLine2'])
- ->setPostalCode($companyValues['PostalCode'])
- ->setCity($companyValues['city'])
- ->setCountry($companyValues['country'])
- ->setPhone($companyValues['phone'])
- ->setEmail($companyValues['email']);
- $this->em->persist($company);
- return $company->getID();
- }
- private function newUser(int $companyID): int
- {
- $user = new User();
- $user->setEmail(uniqid() . '@bhinternet.fr');
- $user->setName('John');
- $user->setPhone('08032510277');
- $encodedPassword = $this->passwordEncoder->encodePassword(
- $user,
- 'Pa55word'
- );
- $tokenValidTill = (new DateTime())->modify('+1 day');
- $user->setPassword($encodedPassword);
- $user->setRoles(['ROLE_ADMIN']);
- $user->setCompany($companyID);
- $user->setPasswordResetToken('123456789101');
- $user->setPasswordResetTokenExpiration($tokenValidTill);
- $this->em->persist($user);
- return $user->getID();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement