Passed
Push — master ( 029f14...93f3b9 )
by Angel Fernando Quiroz
06:50
created

AccessUserFixtures   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 61
c 2
b 1
f 0
dl 0
loc 87
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 3 1
B load() 0 74 1
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\Tool\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
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
        $container = $this->container;
34
        $toolChain = $container->get(ToolChain::class);
35
        $toolChain->createTools();
36
37
        // Defined in AccessGroupFixtures.php.
38
        // $group = $this->getReference('GROUP_ADMIN');
39
40
        $admin = (new User())
41
            ->setSkipResourceNode(true)
42
            ->setLastname('Doe')
43
            ->setFirstname('Joe')
44
            ->setUsername('admin')
45
            ->setStatus(1)
46
            ->setPlainPassword('admin')
47
            ->setEmail('[email protected]')
48
            ->setOfficialCode('ADMIN')
49
            ->setCreatorId(1)
50
            ->setTimezone($timezone)
51
            ->addUserAsAdmin()
52
            ->addRole('ROLE_GLOBAL_ADMIN') // Only for the first user
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
            ->setTimezone($timezone)
73
        ;
74
        $manager->persist($anon);
75
76
        $fallbackUser = new User();
77
        $fallbackUser
78
            ->setSkipResourceNode(true)
79
            ->setUsername('fallback_user')
80
            ->setEmail('[email protected]')
81
            ->setPlainPassword('fallback_user')
82
            ->setStatus(User::ROLE_FALLBACK)
83
            ->setLastname('Fallback')
84
            ->setFirstname('User')
85
            ->setCreatorId(1)
86
            ->setOfficialCode('FALLBACK')
87
            ->setAuthSource(PLATFORM_AUTH_SOURCE)
88
            ->setPhone('0000000000')
89
            ->setTimezone($timezone)
90
            ->setActive(USER_SOFT_DELETED)
91
        ;
92
        $manager->persist($fallbackUser);
93
94
        $manager->flush();
95
96
        $userRepo->addUserToResourceNode($admin->getId(), $admin->getId());
97
        $userRepo->addUserToResourceNode($anon->getId(), $admin->getId());
98
        $userRepo->addUserToResourceNode($fallbackUser->getId(), $admin->getId());
99
100
        $manager->flush();
101
102
        $this->addReference(self::ADMIN_USER_REFERENCE, $admin);
103
        $this->addReference(self::ANON_USER_REFERENCE, $anon);
104
    }
105
}
106