Passed
Pull Request — master (#58)
by Daniel
06:00
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\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
            $queryBuilder->setParameter(':path_' . $pathIndex, $path);
61 1
            if (!\count($filterQueries)) {
62
                $queryBuilder
63 1
                    ->orWhere(
64 1
                        $expr->eq('f.path', ':path_' . $pathIndex)
65
                    );
66 1
                continue;
67
            }
68
69
            $queryBuilder
70 1
                ->orWhere(
71 1
                    $expr->andX(
72 1
                        $expr->eq('f.path', ':path_' . $pathIndex),
73 1
                        $expr->orX(...$filterQueries)
74
                    )
75
                );
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