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

FileInfoRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 44
ccs 24
cts 24
cp 1
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findByPathsAndFilters() 0 27 4
A findOneByPathAndFilter() 0 5 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\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 2
    public function __construct(ManagerRegistry $registry)
31
    {
32 2
        parent::__construct($registry, FileInfo::class);
33 2
    }
34
35
    /**
36
     * @return FileInfo[]
37
     */
38 1
    public function findByPathsAndFilters(array $paths, array $filters): array
39
    {
40 1
        $queryBuilder = $this->createQueryBuilder('f');
41 1
        $expr = $queryBuilder->expr();
42
43 1
        $filterQueries = [];
44 1
        foreach ($filters as $filterIndex => $filter) {
45 1
            if (!$filter) {
46 1
                $filterQueries[] = $expr->isNull('f.filter');
47 1
                continue;
48
            }
49 1
            $filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex);
50 1
            $queryBuilder->setParameter(':filter_' . $filterIndex, $filter);
51
        }
52
53 1
        foreach ($paths as $pathIndex => $path) {
54
            $queryBuilder
55 1
                ->orWhere(
56 1
                    $expr->andX(
57 1
                        $expr->eq('f.path', ':path_' . $pathIndex),
58 1
                        $expr->orX(...$filterQueries)
59
                    )
60
                );
61 1
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
62
        }
63
64 1
        return $queryBuilder->getQuery()->getResult();
65
    }
66
67 1
    public function findOneByPathAndFilter(string $path, ?string $filter): ?FileInfo
68
    {
69 1
        return $this->findOneBy([
70 1
            'path' => $path,
71 1
            'filter' => $filter,
72
        ]);
73
    }
74
}
75