Passed
Pull Request — master (#1930)
by Janko
14:58 queued 05:13
created

LocationMiningRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 35
c 2
b 0
f 1
dl 0
loc 66
ccs 0
cts 38
cp 0
rs 10
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Override;
9
use Stu\Orm\Entity\MiningQueue;
10
use Stu\Orm\Entity\MiningQueueInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
use Stu\Orm\Entity\LocationMining;
13
use Stu\Orm\Entity\LocationMiningInterface;
14
15
/**
16
 * @extends EntityRepository<LocationMining>
17
 */
18
final class LocationMiningRepository extends EntityRepository implements LocationMiningRepositoryInterface
19
{
20
    public const int ISM_RECREATION_COOLDOWN = 1728000; // 20 days
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 20 at column 21
Loading history...
21
22
    #[Override]
23
    public function prototype(): LocationMiningInterface
24
    {
25
        return new LocationMining();
26
    }
27
28
    #[Override]
29
    public function save(LocationMiningInterface $locationMining): void
30
    {
31
        $em = $this->getEntityManager();
32
        $em->persist($locationMining);
33
    }
34
35
    #[Override]
36
    public function getMiningAtLocation(ShipInterface $ship): array
37
    {
38
        return $this->getEntityManager()->createQuery(
39
            sprintf(
40
                'SELECT ml FROM %s ml
41
                 WHERE ml.location_id = :locationId',
42
                LocationMining::class
43
            )
44
        )->setParameters([
45
            'locationId' => $ship->getLocation()->getId()
46
        ])->getResult();
47
    }
48
49
    #[Override]
50
    public function getMiningQueueAtLocation(ShipInterface $ship): ?MiningQueueInterface
51
    {
52
        return $this->getEntityManager()->createQuery(
53
            sprintf(
54
                'SELECT mq FROM %s mq
55
                 WHERE mq.ship_id = :shipId',
56
                MiningQueue::class
57
            )
58
        )->setParameters([
59
            'shipId' => $ship->getId()
60
        ])->getOneOrNullResult();
61
    }
62
63
    #[Override]
64
    public function findById(int $id): ?LocationMiningInterface
65
    {
66
        return $this->find($id);
67
    }
68
69
    /**
70
     * @return LocationMiningInterface[]
71
     */
72
    #[Override]
73
    public function findDepletedEntries(): array
74
    {
75
        $twentyDaysAgo = time() - self::ISM_RECREATION_COOLDOWN;
76
77
        return $this->getEntityManager()->createQuery(
78
            sprintf(
79
                'SELECT lm FROM %s lm
80
                 WHERE lm.depleted_at < :twentyDaysAgo
81
                 AND lm.actual_amount < lm.max_amount',
82
                LocationMining::class
83
            )
84
        )->setParameters([
85
            'twentyDaysAgo' => $twentyDaysAgo
86
        ])->getResult();
87
    }
88
}
89