Doctrine::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4
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