forked from sonata-project/SonataUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserManager.php
145 lines (125 loc) · 3.7 KB
/
UserManager.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\UserBundle\Entity;
use FOS\UserBundle\Doctrine\UserManager as BaseUserManager;
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\UserBundle\Model\UserInterface;
use Sonata\UserBundle\Model\UserManagerInterface;
/**
* @author Hugo Briand <briand@ekino.com>
*/
class UserManager extends BaseUserManager implements UserManagerInterface, ManagerInterface
{
/**
* {@inheritdoc}
*/
public function findUsersBy(?array $criteria = null, ?array $orderBy = null, $limit = null, $offset = null)
{
return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return $this->getRepository()->find($id);
}
/**
* {@inheritdoc}
*/
public function findAll()
{
return $this->getRepository()->findAll();
}
/**
* {@inheritdoc}
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null)
{
return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* {@inheritdoc}
*/
public function findOneBy(array $criteria, ?array $orderBy = null)
{
return parent::findUserBy($criteria);
}
/**
* {@inheritdoc}
*/
public function create()
{
return parent::createUser();
}
/**
* {@inheritdoc}
*/
public function save($entity, $andFlush = true): void
{
if (!$entity instanceof UserInterface) {
throw new \InvalidArgumentException('Save method expected entity of type UserInterface');
}
parent::updateUser($entity, $andFlush);
}
/**
* {@inheritdoc}
*/
public function delete($entity, $andFlush = true): void
{
if (!$entity instanceof UserInterface) {
throw new \InvalidArgumentException('Save method expected entity of type UserInterface');
}
parent::deleteUser($entity);
}
/**
* {@inheritdoc}
*/
public function getTableName()
{
return $this->objectManager->getClassMetadata($this->getClass())->table['name'];
}
/**
* {@inheritdoc}
*/
public function getConnection()
{
return $this->objectManager->getConnection();
}
/**
* {@inheritdoc}
*/
public function getPager(array $criteria, int $page, int $limit = 10, array $sort = []): PagerInterface
{
$query = $this->getRepository()
->createQueryBuilder('u')
->select('u');
$fields = $this->objectManager->getClassMetadata($this->getClass())->getFieldNames();
foreach ($sort as $field => $direction) {
if (!\in_array($field, $fields, true)) {
throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->getClass()));
}
}
if (0 === \count($sort)) {
$sort = ['username' => 'ASC'];
}
foreach ($sort as $field => $direction) {
$query->orderBy(sprintf('u.%s', $field), strtoupper($direction));
}
if (isset($criteria['enabled'])) {
$query->andWhere('u.enabled = :enabled');
$query->setParameter('enabled', $criteria['enabled']);
}
return Pager::create($query, $limit, $page);
}
}