Completed
Pull Request — master (#26)
by Valentyn
02:41
created

UsersFixtures   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 77
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4
ccs 0
cts 40
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 15 1
A createUser() 0 17 2
A createAdmin() 0 18 2
A createTestApiToken() 0 4 1
A createEmailConfirmationToken() 0 5 1
1
<?php
2
3
namespace App\Users\DataFixtures;
4
5
use App\Users\Entity\ConfirmationToken;
6
use App\Users\Entity\User;
7
use Doctrine\Bundle\FixturesBundle\Fixture;
8
use Doctrine\Common\Persistence\ObjectManager;
9
10
class UsersFixtures extends Fixture
11
{
12
    const TESTER_EMAIL = '[email protected]';
13
    const TESTER_USERNAME = 'tester_fixture';
14
    const TESTER_PASSWORD = '123456';
15
    const TESTER_API_TOKEN = 'tester_api_token';
16
    const TESTER_EMAIL_CONFIRMATION_TOKEN = '11kYJ3ut7aOISPQN0RSqceYDasNnb690';
17
18
    const ADMIN_EMAIL = '[email protected]';
19
    const ADMIN_USERNAME = 'admin_fixture';
20
    const ADMIN_PASSWORD = '12345678';
21
    const ADMIN_API_TOKEN = 'admin_api_token';
22
23
    public function load(ObjectManager $manager): void
24
    {
25
        $user = $this->createUser();
26
        $admin = $this->createAdmin();
27
28
        $manager->persist($user);
29
        $manager->persist($admin);
30
        $manager->flush();
31
32
        // Tester
33
        $this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager);
34
        $this->createEmailConfirmationToken($user, self::TESTER_EMAIL_CONFIRMATION_TOKEN, $manager);
35
        // Admin
36
        $this->createTestApiToken($admin, self::ADMIN_API_TOKEN, $manager);
37
    }
38
39
    private function createUser()
40
    {
41
        $user = new User();
42
        $user->username = self::TESTER_USERNAME;
43
        $user->email = self::TESTER_EMAIL;
44
        $user->setPlainPassword(self::TESTER_PASSWORD);
45
46
        $profile = $user->getProfile();
47
        $profile->first_name = 'First';
48
        $profile->last_name = 'Last';
49
50
        for ($i = 3; $i--> 0;) {
51
            $profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info");
52
        }
53
54
        return $user;
55
    }
56
57
    private function createAdmin()
58
    {
59
        $user = new User();
60
        $user->username = self::ADMIN_USERNAME;
61
        $user->email = self::ADMIN_PASSWORD;
62
        $user->setPlainPassword(self::ADMIN_PASSWORD);
63
        $user->addRole(User::ROLE_ADMIN);
64
65
        $profile = $user->getProfile();
66
        $profile->first_name = 'Admin';
67
        $profile->last_name = 'Admin';
68
69
        for ($i = 3; $i--> 0;) {
70
            $profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info");
71
        }
72
73
        return $user;
74
    }
75
76
    private function createTestApiToken(User $user, string $token, ObjectManager $manager): void
77
    {
78
        $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...
79
    }
80
81
    private function createEmailConfirmationToken(User $user, string $token, ObjectManager $manager): void
82
    {
83
        $type = ConfirmationToken::TYPE_CONFIRM_EMAIl;
84
        $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...
85
    }
86
}