Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: hyst
- * Date: 29.05.17
- * Time: 11:48
- */
- namespace AdminBundle\Command;
- use AdminBundle\Entity\BoxStatus;
- use AdminBundle\Entity\PaymentStatus;
- use AdminBundle\Entity\Role;
- use AdminBundle\Entity\TaskStatus;
- use AdminBundle\Entity\User;
- use Doctrine\ORM\EntityManager;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- class InitialCommand extends ContainerAwareCommand
- {
- public function execute(InputInterface $input, OutputInterface $output)
- {
- $em = $this->getContainer()->get('doctrine.orm.entity_manager');
- //TODO: Finish this COMMAND
- $em = $this->checkConnect($em);
- $this->addStaffStatuses($em, $output);
- $em = $this->checkConnect($em);
- $this->addRoles($em, $output);
- $em = $this->checkConnect($em);
- $this->addAdmin($em, $output);
- $em = $this->checkConnect($em);
- $this->addPaymentStatuses($em, $output);
- $em = $this->checkConnect($em);
- $this->addBoxStatuses($em, $output);
- }
- /**
- * @param $em EntityManager
- * @return EntityManager
- */
- public function checkConnect($em)
- {//Connection drops when doctrine see duplicates
- if (!$em->isOpen()) {
- $em = $em->create(
- $em->getConnection(),
- $em->getConfiguration()
- );
- }
- return $em;
- }
- public function configure()
- {
- $this->setName("app:init");
- $this->setDescription("Set default values (admin, roles, statuses, etc.) ");
- }
- /**
- * @param $em EntityManager
- * @param $output OutputInterface
- */
- public function addRoles($em, $output)
- {
- $rolesArray = ['ROLE_SUPPORT' => 'support', 'ROLE_STAFFER' => 'staffers', 'ROLE_SUPER_ADMIN' => 'super', 'ROLE_FINANCE' => 'finance'];
- foreach ($rolesArray as $type => $name) {
- try {
- $role = new Role($name, $type);
- $em->persist($role);
- $em->flush();
- $output->writeln("Role (roleType: " . $type . ", roleName: " . $name . ") added");
- } catch (\Exception $exception) {
- $output->writeln("!!! Some problem with add Role (roleType: " . $type . ", roleName: " . $name . "). ");
- }
- }
- }
- /**
- * @param $em EntityManager
- * @param $output OutputInterface
- */
- public function addStaffStatuses($em, $output)
- {
- $staffStatuses = ['New', 'Received', 'Not received', 'Sent', 'Done', 'Archived', 'Ready to sell', 'Sold'];
- foreach ($staffStatuses as $status) {
- try {
- $new = new TaskStatus($status);
- $em->persist($new);
- $em->flush();
- $output->writeln("Staff task status " . $status . " added");
- } catch (\Exception $exception) {
- $output->writeln("!!! Some problem with add staff status '" . $status . "'");
- }
- }
- }
- /**
- * @param $em EntityManager
- * @param $output OutputInterface
- */
- public function addPaymentStatuses($em, $output)
- {
- $staffStatuses = ['New', 'Done'];
- foreach ($staffStatuses as $status) {
- try {
- $new = new PaymentStatus();
- $new->setStatus($status);
- $em->persist($new);
- $em->flush();
- $output->writeln("Payment status " . $status . " added");
- } catch (\Exception $exception) {
- $output->writeln("!!! Some problem with add payment status '" . $status . "'");
- }
- }
- }
- /**
- * @param $em EntityManager
- * @param $output OutputInterface
- */
- public function addBoxStatuses($em, $output)
- {
- $boxStatuses = ['New', 'Packed', 'Sent', 'Arrived', 'Completed'];
- foreach ($boxStatuses as $status) {
- try {
- $new = new BoxStatus();
- $new->setName($status);
- $em->persist($new);
- $em->flush();
- $output->writeln("Box status " . $status . " added");
- } catch (\Exception $exception) {
- $output->writeln("!!! Some problem with add box status '" . $status . "'");
- }
- }
- }
- /**
- * @param $em EntityManager
- * @param $output OutputInterface
- */
- public function addAdmin($em, $output)
- {
- $adminRole = $em->getRepository("AdminBundle:Role")->findOneBy(['roleType' => 'ROLE_SUPER_ADMIN']);
- $login = 'super_admin';
- $admin = new User();
- $admin->setLogin($login);
- $admin->setJabber('jabber888@jabb3r.org');
- $admin->setEmail('email888@gmail.com');
- $admin->setStatus(User::USER_ACTIVE);
- $admin->setDateCreate(new \DateTime('now'));
- $admin->setAttempts(0);
- $encoder = $this->getContainer()->get('security.password_encoder');
- $password = '1111';
- $password = $encoder->encodePassword($admin, $password);
- $admin->setPassword($password);
- try {
- $em->persist($admin);
- $em->flush();
- $output->writeln("Admin with login: '".$login."', password: '1111' was added");
- //$adminRole->setUser($admin);
- try {
- $admin->setRole($adminRole);
- $em->persist($admin);
- $em->flush();
- $output->writeln("Role ROLE_SUPER_ADMIN was added for ".$login.".");
- } catch (\Exception $exception) {
- $output->writeln("!!! Problem with role add.");
- }
- } catch (\Exception $exception) {
- $output->writeln("!!! Problem with admin added. ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement