Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

UserFixtures::createUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 25
cp 0
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 9
crap 6

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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