Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.96 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Created by PhpStorm.
  5.  * User: hyst
  6.  * Date: 29.05.17
  7.  * Time: 11:48
  8.  */
  9.  
  10. namespace AdminBundle\Command;
  11.  
  12. use AdminBundle\Entity\BoxStatus;
  13. use AdminBundle\Entity\PaymentStatus;
  14. use AdminBundle\Entity\Role;
  15. use AdminBundle\Entity\TaskStatus;
  16. use AdminBundle\Entity\User;
  17. use Doctrine\ORM\EntityManager;
  18. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21.  
  22. class InitialCommand extends ContainerAwareCommand
  23. {
  24.  
  25.     public function execute(InputInterface $input, OutputInterface $output)
  26.     {
  27.         $em = $this->getContainer()->get('doctrine.orm.entity_manager');
  28.  
  29.         //TODO: Finish this COMMAND
  30.  
  31.         $em = $this->checkConnect($em);
  32.         $this->addStaffStatuses($em, $output);
  33.  
  34.         $em = $this->checkConnect($em);
  35.         $this->addRoles($em, $output);
  36.  
  37.         $em = $this->checkConnect($em);
  38.         $this->addAdmin($em, $output);
  39.  
  40.         $em = $this->checkConnect($em);
  41.         $this->addPaymentStatuses($em, $output);
  42.  
  43.         $em = $this->checkConnect($em);
  44.         $this->addBoxStatuses($em, $output);
  45.  
  46.     }
  47.  
  48.     /**
  49.      * @param $em EntityManager
  50.      * @return EntityManager
  51.      */
  52.     public function checkConnect($em)
  53.     {//Connection drops when doctrine see duplicates
  54.         if (!$em->isOpen()) {
  55.             $em = $em->create(
  56.                 $em->getConnection(),
  57.                 $em->getConfiguration()
  58.             );
  59.         }
  60.         return $em;
  61.     }
  62.  
  63.     public function configure()
  64.     {
  65.         $this->setName("app:init");
  66.         $this->setDescription("Set default values (admin, roles, statuses, etc.) ");
  67.     }
  68.  
  69.     /**
  70.      * @param $em EntityManager
  71.      * @param $output OutputInterface
  72.      */
  73.     public function addRoles($em, $output)
  74.     {
  75.  
  76.         $rolesArray = ['ROLE_SUPPORT' => 'support', 'ROLE_STAFFER' => 'staffers', 'ROLE_SUPER_ADMIN' => 'super', 'ROLE_FINANCE' => 'finance'];
  77.  
  78.         foreach ($rolesArray as $type => $name) {
  79.             try {
  80.                 $role = new Role($name, $type);
  81.                 $em->persist($role);
  82.                 $em->flush();
  83.                 $output->writeln("Role (roleType: " . $type . ", roleName: " . $name . ") added");
  84.             } catch (\Exception $exception) {
  85.                 $output->writeln("!!! Some problem with add Role (roleType: " . $type . ", roleName: " . $name . "). ");
  86.             }
  87.         }
  88.     }
  89.  
  90.  
  91.     /**
  92.      * @param $em EntityManager
  93.      * @param $output OutputInterface
  94.      */
  95.     public function addStaffStatuses($em, $output)
  96.     {
  97.         $staffStatuses = ['New', 'Received', 'Not received', 'Sent', 'Done', 'Archived', 'Ready to sell', 'Sold'];
  98.         foreach ($staffStatuses as $status) {
  99.             try {
  100.                 $new = new TaskStatus($status);
  101.                 $em->persist($new);
  102.                 $em->flush();
  103.                 $output->writeln("Staff task status " . $status . " added");
  104.             } catch (\Exception $exception) {
  105.                 $output->writeln("!!! Some problem with add staff status '" . $status . "'");
  106.             }
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * @param $em EntityManager
  112.      * @param $output OutputInterface
  113.      */
  114.     public function addPaymentStatuses($em, $output)
  115.     {
  116.         $staffStatuses = ['New', 'Done'];
  117.         foreach ($staffStatuses as $status) {
  118.             try {
  119.                 $new = new PaymentStatus();
  120.                 $new->setStatus($status);
  121.                 $em->persist($new);
  122.                 $em->flush();
  123.                 $output->writeln("Payment status " . $status . " added");
  124.             } catch (\Exception $exception) {
  125.                 $output->writeln("!!! Some problem with add payment status '" . $status . "'");
  126.             }
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * @param $em EntityManager
  132.      * @param $output OutputInterface
  133.      */
  134.     public function addBoxStatuses($em, $output)
  135.     {
  136.         $boxStatuses = ['New', 'Packed', 'Sent', 'Arrived', 'Completed'];
  137.         foreach ($boxStatuses as $status) {
  138.             try {
  139.                 $new = new BoxStatus();
  140.                 $new->setName($status);
  141.                 $em->persist($new);
  142.                 $em->flush();
  143.                 $output->writeln("Box status " . $status . " added");
  144.             } catch (\Exception $exception) {
  145.                 $output->writeln("!!! Some problem with add box status '" . $status . "'");
  146.             }
  147.         }
  148.     }
  149.  
  150.  
  151.     /**
  152.      * @param $em EntityManager
  153.      * @param $output OutputInterface
  154.      */
  155.     public function addAdmin($em, $output)
  156.     {
  157.         $adminRole = $em->getRepository("AdminBundle:Role")->findOneBy(['roleType' => 'ROLE_SUPER_ADMIN']);
  158.  
  159.         $login = 'super_admin';
  160.  
  161.         $admin = new User();
  162.         $admin->setLogin($login);
  163.         $admin->setJabber('jabber888@jabb3r.org');
  164.         $admin->setEmail('email888@gmail.com');
  165.         $admin->setStatus(User::USER_ACTIVE);
  166.         $admin->setDateCreate(new \DateTime('now'));
  167.         $admin->setAttempts(0);
  168.  
  169.         $encoder = $this->getContainer()->get('security.password_encoder');
  170.         $password = '1111';
  171.         $password = $encoder->encodePassword($admin, $password);
  172.         $admin->setPassword($password);
  173.  
  174.         try {
  175.             $em->persist($admin);
  176.             $em->flush();
  177.  
  178.             $output->writeln("Admin with login: '".$login."', password: '1111' was added");
  179.  
  180.             //$adminRole->setUser($admin);
  181.             try {
  182.                 $admin->setRole($adminRole);
  183.                 $em->persist($admin);
  184.                 $em->flush();
  185.                 $output->writeln("Role ROLE_SUPER_ADMIN was added for ".$login.".");
  186.             } catch (\Exception $exception) {
  187.                 $output->writeln("!!! Problem with role add.");
  188.             }
  189.         } catch (\Exception $exception) {
  190.             $output->writeln("!!! Problem with admin added. ");
  191.         }
  192.  
  193.  
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement