Passed
Push — master ( 48bfaa...170129 )
by Nico
25:44
created

ColonyScanRepository::truncateByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 12
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\Orm\Entity\Colony;
9
use Stu\Orm\Entity\ColonyScan;
10
use Stu\Orm\Entity\ColonyScanInterface;
11
use Stu\Orm\Entity\PlanetField;
12
use Stu\Orm\Entity\PlanetFieldType;
13
14
/**
15
 * @extends EntityRepository<ColonyScan>
16
 */
17
final class ColonyScanRepository extends EntityRepository implements ColonyScanRepositoryInterface
18
{
19
    public function prototype(): ColonyScanInterface
20
    {
21
        return new ColonyScan();
22
    }
23
24
    public function save(ColonyScanInterface $post): void
25
    {
26
        $em = $this->getEntityManager();
27
28
        $em->persist($post);
29
    }
30
31
    public function delete(ColonyScanInterface $post): void
32
    {
33
        $em = $this->getEntityManager();
34
35
        $em->remove($post);
36
    }
37
38
    public function getByUser(int $userId): array
39
    {
40
        return $this->findBy([
41
            'user_id' => $userId
42
        ]);
43
    }
44
45
    public function truncateByUserId(ColonyScanInterface $userId): void
46
    {
47
        $this->getEntityManager()
48
            ->createQuery(
49
                sprintf(
50
                    'DELETE FROM %s cs WHERE cs.user_id = :userid',
51
                    ColonyScan::class
52
                )
53
            )
54
            ->setParameters([
55
                'userid' => $userId
56
            ])
57
            ->execute();
58
    }
59
60
    public function getSurface(int $colonyId): array
61
    {
62
        return $this->getEntityManager()
63
            ->createQuery(sprintf(
64
                'SELECT c.field_id, c.type_id, c.buildings_id
65
                FROM %s c
66
                WHERE c.colonies_id = :colonyid 
67
                AND c.type_id NOT IN (SELECT cf.field_id FROM %s cf WHERE cf.category = 3)
68
                ORDER BY c.field_id ASC',
69
                PlanetField::class,
70
                PlanetFieldType::class
71
            ))
72
            ->setParameters([
73
                'colonyid' => $colonyId
74
            ])
75
            ->getResult();
76
    }
77
78
    public function getSurfaceArray(int $id): string
79
    {
80
        return strval($this->getEntityManager()
81
            ->createQuery(
82
                sprintf(
83
                    'SELECT cs.mask
84
                    FROM %s cs
85
                    WHERE cs.id = :id',
86
                    ColonyScan::class
87
                )
88
            )
89
            ->setParameters([
90
                'id' => $id
91
            ])
92
            ->getSingleScalarResult());
93
    }
94
95
    public function getSurfaceWidth(int $id): int
96
    {
97
        return (int)$this->getEntityManager()
98
            ->createQuery(
99
                sprintf(
100
                    'SELECT c.surface_width
101
                    FROM %s c
102
                    JOIN %s cs WITH c.id = cs.colony_id
103
                    WHERE cs.id = :id',
104
                    Colony::class,
105
                    ColonyScan::class
106
                )
107
            )
108
            ->setParameters([
109
                'id' => $id
110
            ])
111
            ->getSingleScalarResult();
112
    }
113
114
    public function truncateAllColonyScans(): void
115
    {
116
        $this->getEntityManager()->createQuery(
117
            sprintf(
118
                'DELETE FROM %s cs',
119
                ColonyScan::class
120
            )
121
        )->execute();
122
    }
123
}
124