ApplicationRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addSearch() 0 9 3
A search() 0 12 2
A count() 0 6 1
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 MongoDB\BSON\ObjectId;
9
10
class ApplicationRepository extends DocumentRepository
11
{
12
    public function search(User $user, 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, $user, $query);
22
23
        return $qb->getQuery()->execute();
24
    }
25
26
    public function count(User $user, string $query = null)
27
    {
28
        $qb = $this->createQueryBuilder()->count();
29
        $this->addSearch($qb, $user, $query);
30
31
        return $qb->getQuery()->execute();
32
    }
33
34
    private function addSearch(Builder $qb, User $user, string $query = null, string $type = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

34
    private function addSearch(Builder $qb, User $user, string $query = null, /** @scrutinizer ignore-unused */ string $type = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        if (User::ROLE_USER === $user->getRole()) {
37
            //$qb->field('accesses')->elemMatch($qb->expr()->field('user')->references($user));
38
            $qb->field('accesses')->elemMatch(['user.$id' => new ObjectId($user->getId())]);
39
        }
40
41
        if ($query) {
42
            $qb->field('name')->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

42
            $qb->field('name')->equals(/** @scrutinizer ignore-deprecated */ new \MongoRegex(sprintf('/%s/', $query)));
Loading history...
43
        }
44
    }
45
}
46