Passed
Push — master ( e276d1...cad953 )
by Jan
04:31
created

getPartsCountByField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Repository;
22
23
24
use App\Entity\Base\AbstractPartsContainingDBElement;
25
use App\Entity\Base\PartsContainingRepositoryInterface;
26
use App\Entity\Parts\Part;
27
use Doctrine\Common\Collections\Collection;
28
use Doctrine\Common\Collections\Criteria;
29
30
abstract class AbstractPartsContainingRepository extends StructuralDBElementRepository
31
    implements PartsContainingRepositoryInterface
32
{
33
    /**
34
     * Returns all parts associated with this element.
35
     * @param  AbstractPartsContainingDBElement $element The element for which the parts should be determined.
36
     * @param  array  $order_by The order of the parts. Format ['name' => 'ASC']
37
     * @return Part[]
38
     */
39
    abstract public function getParts(object $element, array $order_by = ['name' => 'ASC']): array;
40
41
    /**
42
     * Gets the count of the parts associated with this element.
43
     * @param AbstractPartsContainingDBElement $element The element for which the parts should be determined.
44
     * @return int
45
     */
46
    abstract public function getPartsCount(object $element): int;
47
48
    protected function getPartsByField(object $element, array $order_by, string $field_name): array
49
    {
50
        if (!$element instanceof AbstractPartsContainingDBElement) {
51
            throw new \InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!');
52
        }
53
54
        $repo = $this->getEntityManager()->getRepository(Part::class);
55
56
        return $repo->findBy([$field_name => $element], $order_by);
57
    }
58
59
    protected function getPartsCountByField(object $element, string $field_name): int
60
    {
61
        if (!$element instanceof AbstractPartsContainingDBElement) {
62
            throw new \InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!');
63
        }
64
65
        $repo = $this->getEntityManager()->getRepository(Part::class);
66
67
        return $repo->count([$field_name => $element]);
68
    }
69
70
}