Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\DataFixtures;
  4.  
  5. use App\Entity\BackLink;
  6. use App\Entity\Company;
  7. use App\Entity\User;
  8. use App\Entity\Website;
  9. use DateTime;
  10. use Doctrine\Bundle\FixturesBundle\Fixture;
  11. use Doctrine\Persistence\ObjectManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14.  
  15. class BackLinkFixtures extends Fixture
  16. {
  17.     private $companyList = [];
  18.     private $websiteList = [];
  19.     private $userList = [];
  20.  
  21.     public function loadFixtures(): void
  22.     {
  23.         // creates list of companies
  24.         $this->createCompanies();
  25.  
  26.         // creates lists of users
  27.         $this->createUsers();
  28.  
  29.         // creates lists of websites
  30.         $this->createWebsites();
  31.  
  32.         for ($i = 0; $i < 10; $i++) {
  33.             // this loop creates 10 back links for for each loop above
  34.             for ($id = 1; $id <= 10; $id++) {
  35.                 $this->createBackLink(
  36.                     true,
  37.                     'Technology' . $id,
  38.                     'www.technology.com' . $id,
  39.                     'Technology Comment' . $id,
  40.                     $this->websiteList[$i],
  41.                     $this->userList[$i]
  42.                 );
  43.             }
  44.         }
  45.     }
  46.  
  47.     private function createBackLink(bool $pulse, string $keyword, string $link, string $comment, int $websiteID, int $userID): void {
  48.         $backLink = new BackLink();
  49.         $backLink->setPulse($pulse);
  50.         $backLink->setCreatedBy($userID);
  51.         $backLink->setComment($comment);
  52.         $backLink->setKeyword($keyword);
  53.         $backLink->setLink($link);
  54.         $backLink->setWebsite($websiteID);
  55.         $this->em->persist($backLink);
  56.         $this->em->flush();
  57.     }
  58.  
  59.     private function createCompanies(): void
  60.     {
  61.         $companies = [
  62.             $google = [
  63.                 "name" => "Google",
  64.                 "siret" => "Siret",
  65.                 "AddressLine1" => "USA",
  66.                 "AddressLine2" => "Texas",
  67.                 "PostalCode" => "21290",
  68.                 "city" => "Texas",
  69.                 "country" => "USA",
  70.                 "phone" => "+13546389",
  71.                 "email" => "google@google.com"
  72.             ],
  73.             $amazon = [
  74.             "name" => "Amazon",
  75.             "siret" => "Siret",
  76.             "AddressLine1" => "USA",
  77.             "AddressLine2" => "Texas",
  78.             "PostalCode" => "21290",
  79.             "city" => "Texas",
  80.             "country" => "USA",
  81.             "phone" => "+13546389",
  82.             "email" => "amazon@amazon.com"
  83.             ],
  84.             $facebook = [
  85.                 "name" => "Facebook",
  86.                 "siret" => "Siret",
  87.                 "AddressLine1" => "USA",
  88.                 "AddressLine2" => "Texas",
  89.                 "PostalCode" => "21290",
  90.                 "city" => "Texas",
  91.                 "country" => "USA",
  92.                 "phone" => "+13546389",
  93.                 "email" => "facebook@facebook.com"
  94.  
  95.         ]];
  96.         for ($i = 0; $i < 3; $i++) {
  97.             array_push($this->companyList, $this->newCompany($companies[$i]));
  98.         }
  99.     }
  100.  
  101.     private function createWebsites(): void
  102.     {
  103.         $urls = [
  104.             'google.com',
  105.             'facebook.com',
  106.             'amazon.com',
  107.             'twitter.com',
  108.             'google.com',
  109.             'facebook.com',
  110.             'amazon.com',
  111.             'twitter.com',
  112.             'google.com',
  113.             'facebook.com'
  114.         ];
  115.         for ($i = 0; $i < 10; $i++) {
  116.             array_push($this->websiteList, $this->newWebsite($this->companyList[mt_rand(0, count($this->companyList) - 1)], $urls[$i]));
  117.         }
  118.     }
  119.  
  120.     private function createUsers(): void
  121.     {
  122.         for ($i = 0; $i < 2; $i++) {
  123.             $companyArrayIndex = [0, 2];
  124.             array_push($this->userList, $this->newUser($this->companyList[$companyArrayIndex[$i]]));
  125.         }
  126.     }
  127.  
  128.     private function newWebsite(int $companyID, string $url): int
  129.     {
  130.         $website = new Website();
  131.         $website->setCompany($companyID)
  132.             ->setUrl($url);
  133.         $this->em->persist($website);
  134.         $this->em->flush();
  135.         return $website->getID();
  136.     }
  137.  
  138.     private function newCompany(Company $companyValues): int
  139.     {
  140.         $company = new Company();
  141.         $company->setName($companyValues['name'])
  142.             ->setSiret($companyValues['siret'])
  143.             ->setAddressLine1($companyValues['AddressLine1'])
  144.             ->setAddressLine2($companyValues['AddressLine2'])
  145.             ->setPostalCode($companyValues['PostalCode'])
  146.             ->setCity($companyValues['city'])
  147.             ->setCountry($companyValues['country'])
  148.             ->setPhone($companyValues['phone'])
  149.             ->setEmail($companyValues['email']);
  150.         $this->em->persist($company);
  151.         return $company->getID();
  152.     }
  153.  
  154.     private function newUser(int $companyID): int
  155.     {
  156.         $user = new User();
  157.         $user->setEmail(uniqid() . '@bhinternet.fr');
  158.         $user->setName('John');
  159.         $user->setPhone('08032510277');
  160.  
  161.         $encodedPassword = $this->passwordEncoder->encodePassword(
  162.             $user,
  163.             'Pa55word'
  164.         );
  165.         $tokenValidTill = (new DateTime())->modify('+1 day');
  166.  
  167.         $user->setPassword($encodedPassword);
  168.         $user->setRoles(['ROLE_ADMIN']);
  169.         $user->setCompany($companyID);
  170.         $user->setPasswordResetToken('123456789101');
  171.         $user->setPasswordResetTokenExpiration($tokenValidTill);
  172.         $this->em->persist($user);
  173.         return $user->getID();
  174.     }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement