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) |
|
|
|
|
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
|
|
|
|
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.