Completed
Push — master ( 34b48c...15189e )
by Valentyn
03:16 queued 01:16
created

UsersFixtures::createUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\ConfirmationToken;
6
use App\Entity\User;
7
use App\Entity\UserProfile;
8
use App\Entity\UserProfileContacts;
9
use Doctrine\Bundle\FixturesBundle\Fixture;
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
12
13
class UsersFixtures extends Fixture
14
{
15
    const TESTER_EMAIL = '[email protected]';
16
    const TESTER_USERNAME = 'tester_fixture';
17
    const TESTER_PASSWORD = '123456';
18
    const TESTER_API_TOKEN = 'tester_api_token';
19
    const TESTER_EMAIL_CONFIRMATION_TOKEN = '11kYJ3ut7aOISPQN0RSqceYDasNnb690';
20
21
    const ADMIN_EMAIL = '[email protected]';
22
    const ADMIN_USERNAME = 'admin_fixture';
23
    const ADMIN_PASSWORD = '12345678';
24
    const ADMIN_API_TOKEN = 'admin_api_token';
25
26
    public function load(ObjectManager $manager): void
27
    {
28
        $user = $this->createUser();
29
        $admin = $this->createAdmin();
30
31
        $manager->persist($user);
32
        $manager->persist($admin);
33
        $manager->flush();
34
35
        // Tester
36
        $this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager);
37
        $this->createEmailConfirmationToken($user, self::TESTER_EMAIL_CONFIRMATION_TOKEN, $manager);
38
        // Admin
39
        $this->createTestApiToken($admin, self::ADMIN_API_TOKEN, $manager);
40
    }
41
42
    private function createUser()
43
    {
44
        $user = new User();
45
        $user->username = self::TESTER_USERNAME;
46
        $user->email = self::TESTER_EMAIL;
47
        $user->setPlainPassword(self::TESTER_PASSWORD);
48
49
        $profile = $user->getProfile();
50
        $profile->first_name = 'First';
51
        $profile->last_name = 'Last';
52
53
        for ($i = 3; $i--> 0;) {
54
            $profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info");
55
        }
56
57
        return $user;
58
    }
59
60
    private function createAdmin()
61
    {
62
        $user = new User();
63
        $user->username = self::ADMIN_USERNAME;
64
        $user->email = self::ADMIN_PASSWORD;
65
        $user->setPlainPassword(self::ADMIN_PASSWORD);
66
        $user->addRole(User::ROLE_ADMIN);
67
68
        $profile = $user->getProfile();
69
        $profile->first_name = 'Admin';
70
        $profile->last_name = 'Admin';
71
72
        for ($i = 3; $i--> 0;) {
73
            $profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info");
74
        }
75
76
        return $user;
77
    }
78
79
    private function createTestApiToken(User $user, string $token, ObjectManager $manager): void
80
    {
81
        $manager->getConnection()->exec("INSERT INTO users_api_tokens (id, user_id, token) VALUES (NEXTVAL('users_api_tokens_id_seq'), {$user->getId()}, '{$token}');");
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConnection() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
82
    }
83
84
    private function createEmailConfirmationToken(User $user, string $token, ObjectManager $manager): void
85
    {
86
        $type = ConfirmationToken::TYPE_CONFIRM_EMAIl;
87
        $manager->getConnection()->exec("INSERT INTO users_confirmation_tokens (id, user_id, token, type) VALUES (NEXTVAL('users_confirmation_tokens_id_seq'), {$user->getId()}, '{$token}', '{$type}');");
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConnection() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
    }
89
}