Completed
Push — master ( 78e25f...1ef0da )
by Julien
05:38
created

ProviderRepository::findUpdateInProgress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Document\Provider;
6
use Doctrine\MongoDB\Query\Builder;
7
use Doctrine\ODM\MongoDB\DocumentRepository;
8
9
class ProviderRepository extends DocumentRepository
10
{
11
    public function search(int $page = 1, string $query = null, string $type = null, int $size = 20)
12
    {
13
        $skip = ($page * $size) - $size;
14
        $skip = $skip < 0 ? 0 : $skip;
15
16
        $qb = $this->createQueryBuilder()
17
            ->select('name', 'type', 'vcsUri', 'lastUpdate', 'description', 'updateInProgress', 'downloads')
18
            ->limit($size)
19
            ->skip($skip);
20
21
        $this->addSearch($qb, $query, $type);
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 facets(string $query = null)
35
    {
36
        $qb = $this->createAggregationBuilder()
37
            ->match()
38
            ->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

38
            ->field('name')->equals(/** @scrutinizer ignore-deprecated */ new \MongoRegex(sprintf('/%s/', $query)))
Loading history...
39
            ->group()
40
            ->field('_id')
41
            ->expression('$type')
42
            ->field('count')
43
            ->sum(1);
44
45
        $data = $qb->execute();
46
47
        $rs = [];
48
49
        foreach ($data as $item) {
50
            $rs[$item['_id']] = $item['count'];
51
        }
52
53
        return $rs;
54
    }
55
56
    public function getProvidersAndSha256(): array
57
    {
58
        $providers = $this->createQueryBuilder()
59
            ->select('sha256')
60
            ->hydrate(false)
61
            ->getQuery()
62
            ->execute()
63
            ->toArray();
64
65
        return array_map(function ($item) {
66
            unset($item['_id']);
67
68
            return $item;
69
        }, $providers);
70
    }
71
72
    public function findUpdateInProgress(): array
73
    {
74
        $providers = $this->createQueryBuilder()
75
            ->select('name')
76
            ->field('updateInProgress')->equals(true)
77
            ->hydrate(false)
78
            ->getQuery()
79
            ->execute()
80
            ->toArray();
81
82
        return array_keys($providers);
83
    }
84
85
    public function getReplacements(Provider $provider)
86
    {
87
        return $this->createQueryBuilder()
88
            ->field('replace')->equals($provider->getName())
89
            ->getQuery()
90
            ->execute();
91
    }
92
93
    private function addSearch(Builder $qb, string $query = null, string $type = null): void
94
    {
95
        if ($query) {
96
            $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

96
            $qb->field('name')->equals(/** @scrutinizer ignore-deprecated */ new \MongoRegex(sprintf('/%s/', $query)));
Loading history...
97
        }
98
99
        if ($type) {
100
            $qb->field('type')->equals($type);
101
        }
102
    }
103
}
104