Passed
Pull Request — master (#58)
by Daniel
06:28
created

FileInfoRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 58
ccs 30
cts 30
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B findByPathsAndFilters() 0 41 7
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
        if (!\count($paths)) {
41 1
            return [];
42
        }
43
44 1
        $queryBuilder = $this->createQueryBuilder('f');
45 1
        $expr = $queryBuilder->expr();
46
47 1
        $filterQueries = [];
48 1
        if ($filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
49 1
            foreach ($filters as $filterIndex => $filter) {
50 1
                if (!$filter) {
51 1
                    $filterQueries[] = $expr->isNull('f.filter');
52 1
                    continue;
53
                }
54 1
                $filterQueries[] = $expr->eq('f.filter', ':filter_' . $filterIndex);
55 1
                $queryBuilder->setParameter(':filter_' . $filterIndex, $filter);
56
            }
57
        }
58
59 1
        foreach ($paths as $pathIndex => $path) {
60 1
            if ($filterQueries) {
61
                $queryBuilder
62 1
                    ->orWhere(
63 1
                        $expr->andX(
64 1
                            $expr->eq('f.path', ':path_' . $pathIndex),
65 1
                            $expr->orX(...$filterQueries)
66
                        )
67
                    );
68
            } else {
69
                $queryBuilder
70 1
                    ->orWhere(
71 1
                        $expr->eq('f.path', ':path_' . $pathIndex)
72
                    );
73
            }
74
75 1
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
76
        }
77
78 1
        return $queryBuilder->getQuery()->getResult();
79
    }
80
81 1
    public function findOneByPathAndFilter(string $path, ?string $filter): ?FileInfo
82
    {
83 1
        return $this->findOneBy([
84 1
            'path' => $path,
85 1
            'filter' => $filter,
86
        ]);
87
    }
88
}
89