Passed
Push — master ( c6b44f...9adfc9 )
by Nico
52:13 queued 29:13
created

getByColonyAndUserAndBuildMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 25
c 2
b 2
f 0
nc 1
nop 4
dl 0
loc 34
ccs 0
cts 23
cp 0
crap 2
rs 9.52
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Component\Colony\ColonyEnum;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Orm\Entity\Building;
11
use Stu\Orm\Entity\BuildingCommodity;
12
use Stu\Orm\Entity\PlanetField;
13
use Stu\Orm\Entity\PlanetFieldTypeBuilding;
14
use Stu\Orm\Entity\Researched;
15
use Stu\Orm\Entity\ResearchInterface;
16
17
/**
18
 * @extends EntityRepository<Building>
19
 */
20
final class BuildingRepository extends EntityRepository implements BuildingRepositoryInterface
21
{
22
    public function getBuildmenuBuildings(
23
        PlanetFieldHostInterface $host,
24
        int $userId,
25
        int $buildMenu,
26
        int $offset,
27
        int $commodityId = null
28
    ): array {
29
30
        $commodityFilter = $commodityId === null ? '' : sprintf(
31
            'AND EXISTS (SELECT bc.id FROM %s bc WHERE bc.buildings_id = b.id AND bc.commodity_id = %d)',
32
            BuildingCommodity::class,
33
            $commodityId
34
        );
35
36
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...>getId()))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
37
            ->createQuery(
38
                sprintf(
39
                    'SELECT b FROM %s b
40
                    WHERE b.bm_col = :buildMenu
41
                    AND b.view = :viewState
42
                    AND (b.research_id is null OR b.research_id IN (
43
                            SELECT ru.research_id FROM %s ru WHERE ru.user_id = :userId AND ru.aktiv = :activeState
44
                        ) AND b.id IN (
45
                            SELECT fb.buildings_id FROM %s fb WHERE fb.type IN (
46
                                SELECT fd.type_id FROM %s fd WHERE fd.%s = :hostId
47
                            )
48
                        ))
49
                    %s
50
                    ORDER BY b.name',
51
                    Building::class,
52
                    Researched::class,
53
                    PlanetFieldTypeBuilding::class,
54
                    PlanetField::class,
55
                    $host->getPlanetFieldHostColumnIdentifier(),
56
                    $commodityFilter
57
                )
58
            )
59
            ->setMaxResults(ColonyEnum::BUILDMENU_SCROLLOFFSET)
60
            ->setFirstResult($offset)
61
            ->setParameters([
62
                'activeState' => 0,
63
                'viewState' => 1,
64
                'buildMenu' => $buildMenu,
65
                'userId' => $userId,
66
                'hostId' => $host->getId()
67
            ])
68
            ->getResult();
69
    }
70
71
    public function getByResearch(ResearchInterface $research): array
72
    {
73
        return $this->findBy([
74
            'research_id' => $research->getId()
75
        ]);
76
    }
77
}
78