forked from KnpLabs/KnpUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathUserManager.php
More file actions
103 lines (87 loc) · 2.57 KB
/
Copy pathUserManager.php
File metadata and controls
103 lines (87 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Doctrine;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManager as BaseUserManager;
use FOS\UserBundle\Util\CanonicalFieldsUpdater;
use FOS\UserBundle\Util\PasswordUpdaterInterface;
class UserManager extends BaseUserManager
{
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var string
*
* @phpstan-var class-string<UserInterface>
*/
private $class;
/**
* Constructor.
*
* @phpstan-param class-string<UserInterface> $class
*/
public function __construct(PasswordUpdaterInterface $passwordUpdater, CanonicalFieldsUpdater $canonicalFieldsUpdater, ObjectManager $om, string $class)
{
parent::__construct($passwordUpdater, $canonicalFieldsUpdater);
$this->objectManager = $om;
$this->class = $class;
}
public function deleteUser(UserInterface $user): void
{
$this->objectManager->remove($user);
$this->objectManager->flush();
}
/**
* @phpstan-return class-string<UserInterface>
*/
public function getClass(): string
{
if (false !== strpos($this->class, ':')) {
$metadata = $this->objectManager->getClassMetadata($this->class);
$this->class = $metadata->getName();
}
return $this->class;
}
public function findUserBy(array $criteria): ?UserInterface
{
return $this->getRepository()->findOneBy($criteria);
}
/**
* @return iterable<UserInterface>
*/
public function findUsers(): iterable
{
return $this->getRepository()->findAll();
}
public function reloadUser(UserInterface $user): void
{
$this->objectManager->refresh($user);
}
public function updateUser(UserInterface $user, bool $andFlush = true): void
{
$this->updateCanonicalFields($user);
$this->updatePassword($user);
$this->objectManager->persist($user);
if ($andFlush) {
$this->objectManager->flush();
}
}
/**
* @return ObjectRepository<UserInterface>
*/
protected function getRepository(): ObjectRepository
{
return $this->objectManager->getRepository($this->getClass());
}
}