Passed
Push — master ( 62083f...ad8d4a )
by Julito
10:31
created

AccessUrlAdminFixtures::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 40
nc 1
nop 1
dl 0
loc 54
rs 9.28
c 1
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 AccessUrlAdminFixtures extends Fixture implements ContainerAwareInterface
18
{
19
    public const ADMIN_USER_REFERENCE = 'admin-user';
20
    public const ANON_USER_REFERENCE = 'anon';
21
    public const ACCESS_URL_REFERENCE = 'access-url';
22
23
    private ContainerInterface $container;
24
25
    public function setContainer(ContainerInterface $container = null): void
26
    {
27
        $this->container = $container;
28
    }
29
30
    public function load(ObjectManager $manager): void
31
    {
32
        $timezone = 'Europe\Paris';
33
34
        $container = $this->container;
35
36
        $toolChain = $container->get(ToolChain::class);
37
        $toolChain->createTools($manager);
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
        ;
54
55
        $manager->persist($admin);
56
57
        /** @var UserRepository $userRepo */
58
        $userRepo = $container->get(UserRepository::class);
59
        $userRepo->updateUser($admin);
60
61
        $anon = (new User())
62
            ->setSkipResourceNode(true)
63
            ->setLastname('Joe')
64
            ->setFirstname('Anonymous')
65
            ->setUsername('anon')
66
            ->setStatus(ANONYMOUS)
67
            ->setPlainPassword('anon')
68
            ->setEmail('anonymous@localhost')
69
            ->setOfficialCode('anonymous')
70
            ->setCreatorId(1)
71
            ->setAuthSource(PLATFORM_AUTH_SOURCE)
72
            ->setTimezone($timezone)
73
        ;
74
        $manager->persist($anon);
75
        $manager->flush();
76
77
        $userRepo->addUserToResourceNode($admin->getId(), $admin->getId());
78
        $userRepo->addUserToResourceNode($anon->getId(), $admin->getId());
79
80
        $manager->flush();
81
82
        $this->addReference(self::ADMIN_USER_REFERENCE, $admin);
83
        $this->addReference(self::ANON_USER_REFERENCE, $anon);
84
    }
85
}
86