Passed
Push — master ( 0a87d5...5ba6bb )
by Julito
18:12
created

AccessUserFixtures::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 42
nc 1
nop 1
dl 0
loc 56
rs 9.248
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\DataFixtures;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Repository\Node\UserRepository;
11
use Chamilo\CoreBundle\ToolChain;
12
use Doctrine\Bundle\FixturesBundle\Fixture;
13
use Doctrine\Persistence\ObjectManager;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
class AccessUserFixtures extends Fixture implements ContainerAwareInterface
18
{
19
    public const ADMIN_USER_REFERENCE = 'admin';
20
    public const ANON_USER_REFERENCE = 'anon';
21
    public const ACCESS_URL_REFERENCE = 'accessUrl';
22
    private ContainerInterface $container;
23
24
    public function setContainer(ContainerInterface $container = null): void
25
    {
26
        $this->container = $container;
27
    }
28
29
    public function load(ObjectManager $manager): void
30
    {
31
        $timezone = 'Europe\Paris';
32
        $container = $this->container;
33
        $toolChain = $container->get(ToolChain::class);
34
        $toolChain->createTools();
35
36
        // Defined in AccessGroupFixtures.php.
37
        $group = $this->getReference('GROUP_ADMIN');
38
39
        $admin = (new User())
40
            ->setSkipResourceNode(true)
41
            ->setLastname('Doe')
42
            ->setFirstname('Joe')
43
            ->setUsername('admin')
44
            ->setStatus(1)
45
            ->setPlainPassword('admin')
46
            ->setEmail('[email protected]')
47
            ->setOfficialCode('ADMIN')
48
            ->setCreatorId(1)
49
            ->setAuthSource(PLATFORM_AUTH_SOURCE)
50
            ->setTimezone($timezone)
51
            ->addUserAsAdmin()
52
            ->addRole('ROLE_GLOBAL_ADMIN')
53
            ->addGroup($group)
54
        ;
55
56
        $manager->persist($admin);
57
58
        /** @var UserRepository $userRepo */
59
        $userRepo = $container->get(UserRepository::class);
60
        $userRepo->updateUser($admin);
61
62
        $anon = (new User())
63
            ->setSkipResourceNode(true)
64
            ->setLastname('Joe')
65
            ->setFirstname('Anonymous')
66
            ->setUsername('anon')
67
            ->setStatus(ANONYMOUS)
68
            ->setPlainPassword('anon')
69
            ->setEmail('anonymous@localhost')
70
            ->setOfficialCode('anonymous')
71
            ->setCreatorId(1)
72
            ->setAuthSource(PLATFORM_AUTH_SOURCE)
73
            ->setTimezone($timezone)
74
        ;
75
        $manager->persist($anon);
76
        $manager->flush();
77
78
        $userRepo->addUserToResourceNode($admin->getId(), $admin->getId());
79
        $userRepo->addUserToResourceNode($anon->getId(), $admin->getId());
80
81
        $manager->flush();
82
83
        $this->addReference(self::ADMIN_USER_REFERENCE, $admin);
84
        $this->addReference(self::ANON_USER_REFERENCE, $anon);
85
    }
86
}
87