UserRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromGitlab() 0 15 1
A search() 0 12 2
A count() 0 6 1
A addSearch() 0 5 2
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Document\User;
6
use Doctrine\MongoDB\Query\Builder;
7
use Doctrine\ODM\MongoDB\DocumentRepository;
8
use Omines\OAuth2\Client\Provider\GitlabResourceOwner;
9
10
class UserRepository extends DocumentRepository
11
{
12
    public function search(int $page = 1, string $query = null, int $size = 20)
13
    {
14
        $skip = ($page * $size) - $size;
15
        $skip = $skip < 0 ? 0 : $skip;
16
17
        $qb = $this->createQueryBuilder()
18
            ->limit($size)
19
            ->skip($skip);
20
21
        $this->addSearch($qb, $query);
22
23
        return $qb->getQuery()->execute();
24
    }
25
26
    public function count(string $query = null)
27
    {
28
        $qb = $this->createQueryBuilder()->count();
29
        $this->addSearch($qb, $query);
30
31
        return $qb->getQuery()->execute();
32
    }
33
34
    public function createFromGitlab(GitlabResourceOwner $gitlabUser, string $role): User
35
    {
36
        $user = new User();
37
        $user
38
            ->setRole($role)
39
            ->setUsername($gitlabUser->getUsername())
40
            ->setEmail($gitlabUser->getEmail())
41
            ->setType(User::TYPE_GITLAB)
42
            ->setFullName($gitlabUser->getName())
43
            ->setAvatarUrl($gitlabUser->getAvatarUrl());
44
45
        $this->dm->persist($user);
46
        $this->dm->flush();
47
48
        return $user;
49
    }
50
51
    private function addSearch(Builder $qb, string $query = null): void
52
    {
53
        if ($query) {
54
            $qb->addOr($qb->expr()->field('username')->equals(new \MongoRegex(sprintf('/%s/', $query))));
0 ignored issues
show
Deprecated Code introduced by
The class MongoRegex has been deprecated: This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include: MongoRegex ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
            $qb->addOr($qb->expr()->field('username')->equals(/** @scrutinizer ignore-deprecated */ new \MongoRegex(sprintf('/%s/', $query))));
Loading history...
55
            $qb->addOr($qb->expr()->field('fullName')->equals(new \MongoRegex(sprintf('/%s/', $query))));
0 ignored issues
show
Deprecated Code introduced by
The class MongoRegex has been deprecated: This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include: MongoRegex ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
            $qb->addOr($qb->expr()->field('fullName')->equals(/** @scrutinizer ignore-deprecated */ new \MongoRegex(sprintf('/%s/', $query))));
Loading history...
56
        }
57
    }
58
}
59