Passed
Pull Request — master (#58)
by Daniel
05:57
created

FileInfoRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Entity\Core\FileInfo;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 *
23
 * @method FileInfo|null find($id, $lockMode = null, $lockVersion = null)
24
 * @method FileInfo|null findOneBy(array $criteria, array $orderBy = null)
25
 * @method FileInfo[]    findAll()
26
 * @method FileInfo[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
27
 */
28
class FileInfoRepository extends ServiceEntityRepository
29
{
30
    public function __construct(ManagerRegistry $registry)
31
    {
32
        parent::__construct($registry, FileInfo::class);
33
    }
34
35
    /**
36
     * @return FileInfo[]
37
     */
38
    public function findByPathsAndFilters(array $paths, array $filters)
39
    {
40
        $queryBuilder = $this->createQueryBuilder('f');
41
        $expr = $queryBuilder->expr();
42
43
        $filterQueries = [];
44
        foreach ($filters as $filterIndex => $filter) {
45
            $filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex);
46
            $queryBuilder->setParameter(':filter_' . $filterIndex, $filter);
47
        }
48
49
        foreach ($paths as $pathIndex => $path) {
50
            $queryBuilder
51
                ->orWhere(
52
                    $expr->andX(
53
                        $expr->eq('f.path', ':path_' . $pathIndex),
54
                        $expr->orX($filterQueries)
55
                    )
56
                );
57
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
58
        }
59
60
        return $queryBuilder->getQuery()->getResult();
61
    }
62
63
    public function findByPathAndFilter(string $path, ?string $filter): ?FileInfo
64
    {
65
        return $this->findOneBy([
66
            'path' => $path,
67
            'filter' => $filter,
68
        ]);
69
    }
70
}
71