Completed
Push — master ( c9d345...34b48c )
by Valentyn
03:02 queued 01:25
created

UsersFixtures::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 21
rs 9.3142
c 1
b 1
f 0
cc 2
eloc 14
nc 2
nop 1
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
    public function load(ObjectManager $manager): void
22
    {
23
        $user = new User();
24
        $user->username = self::TESTER_USERNAME;
25
        $user->email = self::TESTER_EMAIL;
26
        $user->setPlainPassword(self::TESTER_PASSWORD);
27
28
        $profile = $user->getProfile();
29
        $profile->first_name = 'First';
30
        $profile->last_name = 'Last';
31
32
        for ($i = 3; $i--> 0;) {
33
            $profile->addContacts("TestProvider #{$i}", "https://test.com/{$i}/info");
34
        }
35
36
        $manager->persist($user);
37
        $manager->flush();
38
39
        $this->createTestApiToken($user, self::TESTER_API_TOKEN, $manager);
40
        $this->createEmailConfirmationToken($user, self::TESTER_EMAIL_CONFIRMATION_TOKEN, $manager);
41
    }
42
43
    private function createTestApiToken(User $user, string $token, ObjectManager $manager): void
44
    {
45
        $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...
46
    }
47
48
    private function createEmailConfirmationToken(User $user, string $token, ObjectManager $manager): void
49
    {
50
        $type = ConfirmationToken::TYPE_CONFIRM_EMAIl;
51
        $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...
52
    }
53
}