Passed
Pull Request — master (#1703)
by Tarmo
08:51
created

HealthzRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Repository/HealthzRepository.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Repository;
10
11
use App\Entity\Healthz as Entity;
12
use DateInterval;
13
use DateTimeImmutable;
14
use DateTimeZone;
15
use Doctrine\DBAL\Types\Types;
16
use Doctrine\ORM\NonUniqueResultException;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Exception;
19
use Throwable;
20
21
/**
22
 * Class HealthzRepository
23
 *
24
 * @package App\Repository
25
 * @author TLe, Tarmo Leppänen <[email protected]>
26
 *
27
 * @psalm-suppress LessSpecificImplementedReturnType
28
 * @codingStandardsIgnoreStart
29
 *
30
 * @method Entity|null find(string $id, ?int $lockMode = null, ?int $lockVersion = null)
31
 * @method Entity|null findAdvanced(string $id, string | int | null $hydrationMode = null)
32
 * @method Entity|null findOneBy(array $criteria, ?array $orderBy = null)
33
 * @method Entity[] findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
34
 * @method Entity[] findByAdvanced(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, ?array $search = null)
35
 * @method Entity[] findAll()
36
 *
37
 * @codingStandardsIgnoreEnd
38
 */
39
class HealthzRepository extends BaseRepository
40
{
41
    /**
42
     * @psalm-var class-string
43
     */
44
    protected static string $entityName = Entity::class;
45
46 117
    public function __construct(
47
        protected ManagerRegistry $managerRegistry,
48
    ) {
49
    }
50
51
    /**
52
     * Method to read value from database
53
     *
54
     * @throws NonUniqueResultException
55
     */
56 4
    public function read(): ?Entity
57
    {
58 4
        $query = $this
59 4
            ->createQueryBuilder('h')
60 4
            ->select('h')
61 4
            ->orderBy('h.timestamp', 'DESC')
62 4
            ->setMaxResults(1)
63 4
            ->getQuery();
64
65 4
        return $query->getOneOrNullResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return App\Entity\Healthz|null. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
68
    /**
69
     * Method to write new value to database.
70
     *
71
     * @throws Throwable
72
     */
73 3
    public function create(): Entity
74
    {
75
        // Create new entity
76 3
        $entity = new Entity();
77
78
        // Store entity to database
79 3
        $this->save($entity);
80
81 3
        return $entity;
82
    }
83
84
    /**
85
     * Method to cleanup 'healthz' table.
86
     *
87
     * @throws Exception
88
     */
89 3
    public function cleanup(): int
90
    {
91
        // Determine date
92 3
        $date = (new DateTimeImmutable(timezone: new DateTimeZone('UTC')))
93 3
            ->sub(new DateInterval('P7D'));
94
95
        // Create query builder
96 3
        $queryBuilder = $this
97 3
            ->createQueryBuilder('h')
98 3
            ->delete()
99 3
            ->where('h.timestamp < :timestamp')
100 3
            ->setParameter('timestamp', $date, Types::DATETIME_IMMUTABLE);
101
102
        // Return deleted row count
103 3
        return (int)$queryBuilder->getQuery()->execute();
104
    }
105
}
106