Completed
Push — master ( 4b4e40...af8835 )
by Craig
10:40 queued 04:02
created

getByExpiredPasswords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ZAuthModule\Entity\Repository;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\ORM\Query\Expr\OrderBy;
18
use Doctrine\ORM\Tools\Pagination\Paginator;
19
use Doctrine\Persistence\ManagerRegistry;
20
use Zikula\Bundle\CoreBundle\Doctrine\WhereFromFilterTrait;
21
use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity;
22
use Zikula\ZAuthModule\Entity\RepositoryInterface\AuthenticationMappingRepositoryInterface;
23
24
class AuthenticationMappingRepository extends ServiceEntityRepository implements AuthenticationMappingRepositoryInterface
25
{
26
    use WhereFromFilterTrait;
27
28
    public function __construct(ManagerRegistry $registry)
29
    {
30
        parent::__construct($registry, AuthenticationMappingEntity::class);
31
    }
32
33
    public function persistAndFlush(AuthenticationMappingEntity $entity): void
34
    {
35
        $this->_em->persist($entity);
36
        $this->_em->flush();
37
    }
38
39
    public function removeByZikulaId(int $userId): void
40
    {
41
        $mapping = $this->findOneBy(['uid' => $userId]);
42
        if (isset($mapping)) {
43
            $this->_em->remove($mapping);
44
            $this->_em->flush();
45
        }
46
    }
47
48
    public function getByZikulaId(int $userId): AuthenticationMappingEntity
49
    {
50
        return $this->findOneBy(['uid' => $userId]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findOneBy(array('uid' => $userId)) could return the type null which is incompatible with the type-hinted return Zikula\ZAuthModule\Entit...enticationMappingEntity. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    public function setEmailVerification(int $userId, bool $value = true): void
54
    {
55
        $mapping = $this->findOneBy(['uid' => $userId]);
56
        if (isset($mapping)) {
57
            $mapping->setVerifiedEmail($value);
58
            $this->_em->flush();
59
        }
60
    }
61
62
    /**
63
     * Fetch a collection of users. Optionally filter, sort, limit, offset results.
64
     *   filter = [field => value, field => value, field => ['operator' => '!=', 'operand' => value], ...]
65
     *   when value is not an array, operator is assumed to be '='
66
     *
67
     * @return Paginator|AuthenticationMappingEntity[]
68
     */
69
    public function query(
70
        array $filter = [],
71
        array $sort = [],
72
        int $limit = 0,
73
        int $offset = 0,
74
        string $exprType = 'and'
75
    ) {
76
        $qb = $this->createQueryBuilder('m')
77
            ->select('m');
78
        if (!empty($filter)) {
79
            $where = $this->whereFromFilter($qb, $filter, $exprType, 'm');
80
            $qb->andWhere($where);
81
        }
82
        if (!empty($sort)) {
83
            $qb->orderBy($this->orderByFromArray($sort));
84
        }
85
        $query = $qb->getQuery();
86
87
        if ($limit > 0) {
88
            $query->setMaxResults($limit);
89
            $query->setFirstResult($offset);
90
91
            return new Paginator($query);
92
        }
93
94
        return $query->getResult();
95
    }
96
97
    public function getByExpiredPasswords()
98
    {
99
        $qb = $this->createQueryBuilder('m')
100
            ->select('m')
101
            ->where('m.pass NOT LIKE :param')
102
            ->setParameter('param', '$%');
103
104
        return $qb->getQuery()->getResult();
105
    }
106
107
    /**
108
     * Construct a QueryBuilder Expr\OrderBy object suitable for use in QueryBuilder->orderBy() from an array.
109
     * sort = [field => dir, field => dir, ...]
110
     */
111
    private function orderByFromArray(array $sort = []): OrderBy
112
    {
113
        $orderBy = new OrderBy();
114
        foreach ($sort as $field => $direction) {
115
            $orderBy->add('m.' . $field, $direction);
116
        }
117
118
        return $orderBy;
119
    }
120
}
121