Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

GeneratorBundle/DataFixtures/ORM/UserFixtures.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\DataFixtures\ORM;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Kunstmaan\AdminBundle\Entity\User;
9
use Symfony\Component\Console\Output\ConsoleOutput;
10
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\HttpKernel\Kernel;
13
14
/**
15
 * Fixture for creating the admin and guest user
16
 */
17
class UserFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
18
{
19
    const REFERENCE_ADMIN_USER = 'adminuser';
20
21
    /** @var ContainerInterface */
22
    private $container;
23
24
    /**
25
     * Sets the Container.
26
     *
27
     * @param ContainerInterface|null $container A ContainerInterface instance or null
28
     *
29
     * @api
30
     */
31
    public function setContainer(ContainerInterface $container = null)
32
    {
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * Load data fixtures with the passed EntityManager
38
     *
39
     * @param ObjectManager $manager
40
     */
41
    public function load(ObjectManager $manager)
42
    {
43
        $password = substr(rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '='), 0, 8);
44
45
        $user1 = $this->createUser(
46
            $manager,
47
            'admin',
48
            $password,
49
            '[email protected]',
50
            $this->container->getParameter('kunstmaan_admin.default_admin_locale'),
51
            array('ROLE_SUPER_ADMIN'),
52
            array($manager->merge($this->getReference(GroupFixtures::REFERENCE_SUPERADMINS_GROUP))),
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Persistence\ObjectManager::merge() has been deprecated with message: Merge operation is deprecated and will be removed in Persistence 2.0. Merging should be part of the business domain of an application rather than a generic operation of ObjectManager.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
53
            true,
54
            false
55
        );
56
        $manager->flush();
57
58
        $output = new ConsoleOutput();
59
        $output->writeln(array(
60
            "<comment>  > User 'admin' created with password '$password'</comment>",
61
        ));
62
63
        if (Kernel::VERSION_ID < 40000) {
64
            $file = $this->container->get('kernel')->getProjectDir() . '/app/config/config.yml';
65
            $contents = file_get_contents($file);
66
            $contents = str_replace('-adminpwd-', $password, $contents);
67
            file_put_contents($file, $contents);
68
        }
69
70
        $this->setReference(self::REFERENCE_ADMIN_USER, $user1);
71
    }
72
73
    /**
74
     * Create a user
75
     *
76
     * @param ObjectManager $manager  The object manager
77
     * @param string        $username The username
78
     * @param string        $password The plain password
79
     * @param string        $email    The email of the user
80
     * @param string        $locale   The locale (language) of the user
81
     * @param array         $roles    The roles the user has
82
     * @param array         $groups   The groups the user belongs to
83
     * @param bool          $enabled  Enable login for the user
84
     * @param bool          $changed  Disable password changed for the user
85
     *
86
     * @return User
87
     */
88
    private function createUser(
89
        ObjectManager $manager,
90
        $username,
91
        $password,
92
        $email,
93
        $locale,
94
        array $roles = array(),
95
        array $groups = array(),
96
        $enabled = false,
97
        $changed = false
98
    ) {
99
        $user = $this->container->get('fos_user.user_manager')->createUser();
100
        $user->setUsername($username);
101
        $user->setPlainPassword($password);
102
        $user->setRoles($roles);
103
        $user->setEmail($email);
104
        $user->setEnabled($enabled);
105
        $user->setAdminLocale($locale);
106
        $user->setPasswordChanged($changed);
107
        foreach ($groups as $group) {
108
            $user->addGroup($group);
109
        }
110
111
        $manager->persist($user);
112
113
        return $user;
114
    }
115
116
    /**
117
     * Get the order of this fixture
118
     *
119
     * @return int
120
     */
121
    public function getOrder()
122
    {
123
        return 3;
124
    }
125
}
126