Passed
Push — master ( 95eec6...6e6d6b )
by
unknown
08:59 queued 17s
created

Version20230720142900::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\UserRepository;
12
use Doctrine\DBAL\Schema\Schema;
13
14
use const PASSWORD_DEFAULT;
15
16
class Version20230720142900 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Adds a fallback user to the user table.';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $repo = $this->container->get(UserRepository::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        $repo = $this->container->get(UserRepository::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
        // Check if fallback user already exists
28
        $existingFallbackUser = $repo->findOneBy(['username' => 'fallback_user']);
29
30
        if ($existingFallbackUser) {
31
            // User already exists, no need to create it again
32
            error_log('Fallback user already exists: ' . $existingFallbackUser->getFullname());
33
            return;
34
        }
35
36
        // Create fallback user if not exists
37
        $plainPassword = 'fallback_user';
38
        $encodedPassword = password_hash($plainPassword, PASSWORD_DEFAULT);
39
40
        $fallbackUser = new User();
41
        $fallbackUser
42
            ->setUsername('fallback_user')
43
            ->setEmail('[email protected]')
44
            ->setPassword($encodedPassword)
45
            ->setCreatorId(1)
46
            ->setStatus(User::ROLE_FALLBACK)
47
            ->setLastname('Fallback')
48
            ->setFirstname('User')
49
            ->setOfficialCode('FALLBACK')
50
            ->setAuthSource('platform')
51
            ->setPhone('0000000000')
52
            ->setLocale('en')
53
            ->setActive(User::SOFT_DELETED)
54
            ->setTimezone('UTC')
55
        ;
56
57
        $this->entityManager->persist($fallbackUser);
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $this->entityManager->/** @scrutinizer ignore-call */ 
58
                              persist($fallbackUser);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        $this->entityManager->flush();
59
60
        error_log('Fallback user created: ' . $fallbackUser->getFullname());
61
62
        $repo->updateUser($fallbackUser);
63
    }
64
}
65