Doctrine   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 68
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A findByProviderIdentifier() 0 9 1
A addSocialUser() 0 13 1
1
<?php
2
3
namespace Svycka\SocialUser\Storage;
4
5
use Doctrine\ORM\EntityManager;
6
use Svycka\SocialUser\Entity\SocialUser;
7
use Svycka\SocialUser\Entity\SocialUserInterface;
8
9
/**
10
 * @author Vytautas Stankus <[email protected]>
11
 * @license MIT
12
 */
13
class Doctrine implements SocialUserStorageInterface
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    protected $em;
19
20
    /**
21
     * Social user Entity class name
22
     *
23
     * @var string
24
     */
25
    protected $entity = SocialUser::class;
26
27 4
    public function __construct(EntityManager $entityManager, array $options = null)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
28
    {
29 4
        $options = empty($options) ? [] : $options;
30
31 4
        if (array_key_exists('social_user_entity', $options)) {
32 2
            if (!is_subclass_of($options['social_user_entity'], SocialUserInterface::class)) {
33 1
                throw new \Exception(
34 1
                    sprintf('Configured "social_user_entity" class should implement %s', SocialUserInterface::class)
35
                );
36
            }
37 1
            $this->entity = $options['social_user_entity'];
38
        }
39
40 3
        $this->em = $entityManager;
41 3
    }
42
43
    /**
44
     * @param string $provider
45
     * @param string $identifier
46
     *
47
     * @return SocialUserInterface|null
48
     */
49 1
    public function findByProviderIdentifier($provider, $identifier)
50
    {
51 1
        $repository = $this->em->getRepository($this->entity);
52
53 1
        return $repository->findOneBy([
54 1
            'provider' => $provider,
55 1
            'identifier' => $identifier,
56
        ]);
57
    }
58
59
    /**
60
     * @param int $user_id
61
     * @param string $identifier
62
     * @param string $provider
63
     *
64
     * @return SocialUserInterface
65
     * @throws \Doctrine\ORM\ORMException
66
     */
67 1
    public function addSocialUser($user_id, $identifier, $provider)
68
    {
69
        /** @var SocialUserInterface $socialUser */
70 1
        $socialUser = new $this->entity();
71 1
        $socialUser->setLocalUser($user_id);
72 1
        $socialUser->setIdentifier($identifier);
73 1
        $socialUser->setProvider($provider);
74
75 1
        $this->em->persist($socialUser);
76 1
        $this->em->flush();
77
78 1
        return $socialUser;
79
    }
80
}
81