Passed
Push — master ( a7ed20...e72196 )
by Daniel
08:57 queued 03:23
created

FileInfoRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 2
    public function __construct(ManagerRegistry $registry)
33
    {
34 2
        parent::__construct($registry, FileInfo::class);
35 2
    }
36
37 1
    public function findOneByPathAndFilter(string $path, ?string $filter): ?FileInfo
38
    {
39 1
        return $this->findOneBy([
40 1
            'path' => $path,
41 1
            'filter' => $filter,
42
        ]);
43
    }
44
45
    /**
46
     * @return FileInfo[]
47
     */
48 1
    public function findByPathsAndFilters(array $paths, ?array $filters): array
49
    {
50 1
        if (!\count($paths)) {
51 1
            return [];
52
        }
53
54 1
        $queryBuilder = $this->createQueryBuilder('f');
55 1
        $expr = $queryBuilder->expr();
56
57 1
        $filterQueries = $this->getFilterQueries($filters, $expr, $queryBuilder);
58 1
        $filterQueryCount = (bool) \count($filterQueries);
59
60 1
        foreach ($paths as $pathIndex => $path) {
61 1
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
62
63 1
            if (!$filterQueryCount) {
64
                $queryBuilder
65 1
                    ->orWhere(
66 1
                        $expr->eq('f.path', ':path_' . $pathIndex)
67
                    );
68 1
                continue;
69
            }
70
71
            $queryBuilder
72 1
                ->orWhere(
73 1
                    $expr->andX(
74 1
                        $expr->eq('f.path', ':path_' . $pathIndex),
75 1
                        $expr->orX(...$filterQueries)
76
                    )
77
                );
78
        }
79
80 1
        return $queryBuilder->getQuery()->getResult();
81
    }
82
83 1
    private function getFilterQueries(?array $filters, Expr $expr, QueryBuilder $queryBuilder): array
84
    {
85 1
        $filterQueries = [];
86 1
        if (null !== $filters) {
0 ignored issues
show
introduced by
The condition null !== $filters is always true.
Loading history...
87 1
            foreach ($filters as $filterIndex => $filter) {
88 1
                if (!$filter) {
89 1
                    $filterQueries[] = $expr->isNull('f.filter');
90 1
                    continue;
91
                }
92 1
                $filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex);
93 1
                $queryBuilder->setParameter(':filter_' . $filterIndex, $filter);
94
            }
95
        }
96
97 1
        return $filterQueries;
98
    }
99
}
100