LogRequestRepository::cleanHistory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Repository/LogRequestRepository.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Repository;
10
11
use App\Entity\LogRequest as Entity;
12
use DateInterval;
13
use DateTimeImmutable;
14
use DateTimeZone;
15
use Doctrine\Persistence\ManagerRegistry;
16
use Exception;
17
18
/**
19
 * Class LogRequestRepository
20
 *
21
 * @package App\Repository
22
 * @author TLe, Tarmo Leppänen <[email protected]>
23
 *
24
 * @psalm-suppress LessSpecificImplementedReturnType
25
 * @codingStandardsIgnoreStart
26
 *
27
 * @method Entity|null find(string $id, ?int $lockMode = null, ?int $lockVersion = null)
28
 * @method Entity|null findAdvanced(string $id, string | int | null $hydrationMode = null)
29
 * @method Entity|null findOneBy(array $criteria, ?array $orderBy = null)
30
 * @method Entity[] findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
31
 * @method Entity[] findByAdvanced(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, ?array $search = null)
32
 * @method Entity[] findAll()
33
 *
34
 * @codingStandardsIgnoreEnd
35
 */
36
class LogRequestRepository extends BaseRepository
37
{
38
    /**
39
     * @psalm-var class-string
40
     */
41
    protected static string $entityName = Entity::class;
42
43 625
    public function __construct(
44
        protected ManagerRegistry $managerRegistry,
45
    ) {
46 625
    }
47
48
    /**
49
     * Helper method to clean history data from request_log table.
50
     *
51
     * @throws Exception
52
     */
53 1
    public function cleanHistory(): int
54
    {
55
        // Determine date
56 1
        $date = (new DateTimeImmutable(timezone: new DateTimeZone('UTC')))
57 1
            ->sub(new DateInterval('P3Y'));
58
59
        // Create query builder and define delete query
60 1
        $queryBuilder = $this
61 1
            ->createQueryBuilder('requestLog')
62 1
            ->delete()
63 1
            ->where('requestLog.date < :date')
64 1
            ->setParameter('date', $date->format('Y-m-d'));
65
66
        // Return deleted row count
67 1
        return (int)$queryBuilder->getQuery()->execute();
68
    }
69
}
70