UserFixtures::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 28
cp 0
rs 9.408
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
        $user1->setCreatedBy('CMS installation');
57
        $manager->flush();
58
59
        $output = new ConsoleOutput();
60
        $output->writeln(array(
0 ignored issues
show
Documentation introduced by
array("<comment> > User...$password}'</comment>") is of type array<integer,?>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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