Passed
Push — master ( 92a2e9...9d73f4 )
by Nico
41:55
created

BuildingCommodityRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 122
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getProductionByCommodityAndUser() 0 25 1
A getProductionByColony() 0 29 1
A getProductionSumForAllUserColonies() 0 28 1
A getByBuilding() 0 5 1
A canProduceCommodity() 0 24 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\ResultSetMapping;
9
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Stu\Component\Colony\ColonyFunctionManager;
11
use Stu\Lib\Colony\PlanetFieldHostInterface;
12
use Stu\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Stu\Orm\Entity\BuildingCommodity;
14
use Stu\Orm\Entity\ColonyClassInterface;
15
use Stu\Orm\Entity\UserInterface;
16
17
/**
18
 * @extends EntityRepository<BuildingCommodity>
19
 */
20
final class BuildingCommodityRepository extends EntityRepository implements BuildingCommodityRepositoryInterface
21
{
22
    #[Override]
23
    public function getByBuilding(int $buildingId): array
24
    {
25
        return $this->findBy([
26
            'buildings_id' => $buildingId
27
        ]);
28
    }
29
30
    #[Override]
31
    public function getProductionByColony(PlanetFieldHostInterface $host, ColonyClassInterface $colonyClass): iterable
32
    {
33
        $rsm = new ResultSetMapping();
34
        $rsm->addScalarResult('commodity_id', 'commodity_id', 'integer');
35
        $rsm->addScalarResult('production', 'production', 'integer');
36
        $rsm->addScalarResult('pc', 'pc', 'integer');
37
38
        return $this->getEntityManager()
39
            ->createNativeQuery(
40
                sprintf(
41
                    'SELECT a.id as commodity_id, COALESCE(SUM(c.count), 0) as production, COALESCE(MAX(d.count),0) as pc
42
                    FROM stu_commodity a
43
                        LEFT JOIN stu_colonies_fielddata b ON b.%s = :hostId AND b.aktiv = :state
44
                        LEFT JOIN stu_buildings_commodity c ON c.commodity_id = a.id AND c.buildings_id = b.buildings_id
45
                        LEFT JOIN stu_planets_commodity d ON d.commodity_id = a.id AND d.planet_classes_id = :colonyClassId
46
                    WHERE c.count != 0 OR d.count != 0
47
                    GROUP BY a.id
48
                    ORDER BY a.sort ASC',
49
                    $host->getPlanetFieldHostColumnIdentifier()
50
                ),
51
                $rsm
52
            )
53
            ->setParameters([
54
                'state' => 1,
55
                'hostId' => $host->getId(),
56
                'colonyClassId' => $colonyClass->getId()
57
            ])
58
            ->getResult();
59
    }
60
61
    #[Override]
62
    public function getProductionSumForAllUserColonies(UserInterface $user): iterable
63
    {
64
        $rsm = new ResultSetMapping();
65
        $rsm->addScalarResult('commodity_id', 'commodity_id', 'integer');
66
        $rsm->addScalarResult('amount', 'amount', 'integer');
67
        $rsm->addScalarResult('commodity_name', 'commodity_name');
68
69
        return $this->getEntityManager()
70
            ->createNativeQuery(
71
                'SELECT a.id as commodity_id, a.name as commodity_name, SUM(c.count) + COALESCE(MAX(d.count),0) as amount
72
                FROM stu_commodity a
73
                    LEFT JOIN stu_colonies col ON col.user_id = :userId
74
                    LEFT JOIN stu_colonies_fielddata b ON b.colonies_id = col.id AND b.aktiv = :state
75
                    LEFT JOIN stu_buildings_commodity c ON c.commodity_id = a.id AND c.buildings_id = b.buildings_id
76
                    LEFT JOIN stu_planets_commodity d ON d.commodity_id = a.id AND d.planet_classes_id = col.colonies_classes_id
77
                WHERE a.type = :commodityType
78
                GROUP BY a.id
79
                HAVING SUM(c.count) + COALESCE(MAX(d.count),0) != 0
80
                ORDER BY a.sort ASC',
81
                $rsm
82
            )
83
            ->setParameters([
84
                'state' => ColonyFunctionManager::STATE_ENABLED,
85
                'commodityType' => CommodityTypeEnum::COMMODITY_TYPE_STANDARD,
86
                'userId' => $user->getId(),
87
            ])
88
            ->toIterable();
89
    }
90
91
    #[Override]
92
    public function getProductionByCommodityAndUser(int $commodityId, UserInterface $user): int
93
    {
94
        $rsm = new ResultSetMapping();
95
        $rsm->addScalarResult('gc', 'gc', 'integer');
96
97
        return (int) $this->getEntityManager()->createNativeQuery(
98
            'SELECT SUM(c.count) as gc
99
            FROM stu_colonies d
100
            LEFT JOIN stu_colonies_fielddata b
101
                ON b.colonies_id = d.id
102
                AND b.aktiv = :state
103
            LEFT JOIN stu_buildings_commodity c
104
                ON c.buildings_id = b.buildings_id
105
            LEFT JOIN stu_commodity a
106
                ON c.commodity_id = a.id
107
            WHERE d.user_id = :userId
108
                AND a.id = :commodityId
109
                AND c.count != 0',
110
            $rsm
111
        )->setParameters([
112
            'state' => 1,
113
            'userId' => $user->getId(),
114
            'commodityId' => $commodityId
115
        ])->getSingleScalarResult();
116
    }
117
118
    public function canProduceCommodity(int $userId, int $commodityId): bool
119
    {
120
        $rsm = new ResultSetMapping();
121
        $rsm->addScalarResult('buildings_id', 'buildings_id', 'integer');
122
123
        $query = $this->getEntityManager()->createNativeQuery(
124
            'SELECT bc.buildings_id
125
            FROM stu_buildings_commodity bc
126
            JOIN stu_buildings b ON bc.buildings_id = b.id
127
            LEFT JOIN stu_researched r ON b.research_id = r.research_id AND r.user_id = :userId AND r.aktiv = 0
128
            WHERE bc.commodity_id = :commodityId
129
            AND bc.count > 0
130
            AND (b.research_id IS NULL OR r.id IS NOT NULL)',
131
            $rsm
132
        );
133
134
        $query->setParameters([
135
            'commodityId' => $commodityId,
136
            'userId' => $userId
137
        ]);
138
139
        $result = $query->getResult();
140
141
        return !empty($result);
142
    }
143
}
144