Test Failed
Pull Request — master (#1775)
by Nico
26:52
created

CommodityRepository::getViewable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Lib\Colony\PlanetFieldHostInterface;
9
use Stu\Module\Commodity\CommodityTypeEnum;
10
use Stu\Orm\Entity\BuildingCommodity;
11
use Stu\Orm\Entity\Commodity;
12
use Stu\Orm\Entity\PlanetField;
13
14
/**
15
 * @extends EntityRepository<Commodity>
16
 */
17
final class CommodityRepository extends EntityRepository implements CommodityRepositoryInterface
18
{
19
    public function getByBuildingsOnColony(PlanetFieldHostInterface $host): array
20
    {
21
        return $this->getEntityManager()
22
            ->createQuery(
23
                sprintf(
24
                    'SELECT c FROM %s c
25
                    WHERE c.id IN (
26
                        SELECT bg.commodity_id
27
                        FROM %s bg
28
                        WHERE bg.buildings_id IN (
29
                            SELECT pf.buildings_id
30
                            FROM %s pf
31
                            WHERE pf.%s = :hostId
32
                        )
33
                    ) ORDER BY c.name ASC',
34
                    Commodity::class,
35
                    BuildingCommodity::class,
36
                    PlanetField::class,
37
                    $host->getPlanetFieldHostColumnIdentifier()
38
                )
39
            )
40
            ->setParameter('hostId', $host->getId())
41
            ->getResult();
42
    }
43
44
    public function getByType(int $typeId): array
45
    {
46
        return $this->findBy([
47
            'type' => $typeId
48
        ], ['sort' => 'asc']);
49
    }
50
51
    public function getViewable(): array
52
    {
53
        return $this->findBy([
54
            'view' => true
55
        ], ['sort' => 'asc']);
56
    }
57
58
    public function getTradeable(): array
59
    {
60
        return $this->findBy([
61
            'view' => true,
62
            'npc_commodity' => false,
63
            'type' => CommodityTypeEnum::COMMODITY_TYPE_STANDARD
64
        ], ['sort' => 'asc']);
65
    }
66
67
    public function getTradeableNPC(): array
68
    {
69
        return $this->findBy([
70
            'view' => true,
71
            'type' => CommodityTypeEnum::COMMODITY_TYPE_STANDARD
72
        ], ['sort' => 'asc']);
73
    }
74
75
    public function getAll(): array
76
    {
77
        return $this->getEntityManager()
78
            ->createQuery(
79
                sprintf(
80
                    'SELECT c FROM %s c
81
                    INDEX BY c.id
82
                    ORDER BY c.sort ASC',
83
                    Commodity::class
84
                )
85
            )
86
            ->getResult();
87
    }
88
}
89