FileInfoRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 63
ccs 0
cts 35
cp 0
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilterQueries() 0 15 4
A __construct() 0 3 1
A findByPathsAndFilters() 0 26 4
A findOneByPathAndFilter() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Repository\Core;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\ORM\Query\Expr;
18
use Doctrine\ORM\QueryBuilder;
19
use Doctrine\Persistence\ManagerRegistry;
20
use Silverback\ApiComponentsBundle\Entity\Core\FileInfo;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 *
25
 * @method FileInfo|null find($id, $lockMode = null, $lockVersion = null)
26
 * @method FileInfo|null findOneBy(array $criteria, array $orderBy = null)
27
 * @method FileInfo[]    findAll()
28
 * @method FileInfo[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
29
 */
30
class FileInfoRepository extends ServiceEntityRepository
31
{
32
    public function __construct(ManagerRegistry $registry)
33
    {
34
        parent::__construct($registry, FileInfo::class);
35
    }
36
37
    public function findOneByPathAndFilter(string $path, ?string $filter): ?FileInfo
38
    {
39
        return $this->findOneBy(
40
            [
41
                'path' => $path,
42
                'filter' => $filter,
43
            ]
44
        );
45
    }
46
47
    /**
48
     * @return FileInfo[]
49
     */
50
    public function findByPathsAndFilters(array $paths, ?array $filters): array
51
    {
52
        if (!\count($paths)) {
53
            return [];
54
        }
55
56
        $queryBuilder = $this->createQueryBuilder('f');
57
        $expr = $queryBuilder->expr();
58
59
        $filterQueries = $this->getFilterQueries($filters, $expr, $queryBuilder);
60
        $filterQueryCount = (bool) \count($filterQueries);
61
62
        foreach ($paths as $pathIndex => $path) {
63
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
64
65
            if (!$filterQueryCount) {
66
                $queryBuilder
67
                    ->orWhere($expr->eq('f.path', ':path_' . $pathIndex));
68
                continue;
69
            }
70
71
            $queryBuilder
72
                ->orWhere($expr->andX($expr->eq('f.path', ':path_' . $pathIndex), $expr->orX(...$filterQueries)));
73
        }
74
75
        return $queryBuilder->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryBuilder->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
76
    }
77
78
    private function getFilterQueries(?array $filters, Expr $expr, QueryBuilder $queryBuilder): array
79
    {
80
        $filterQueries = [];
81
        if (null !== $filters) {
0 ignored issues
show
introduced by
The condition null !== $filters is always true.
Loading history...
82
            foreach ($filters as $filterIndex => $filter) {
83
                if (!$filter) {
84
                    $filterQueries[] = $expr->isNull('f.filter');
85
                    continue;
86
                }
87
                $filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex);
88
                $queryBuilder->setParameter(':filter_' . $filterIndex, $filter);
89
            }
90
        }
91
92
        return $filterQueries;
93
    }
94
}
95