|
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
|
|
|
public function __construct( |
|
44
|
|
|
protected ManagerRegistry $managerRegistry, |
|
45
|
|
|
) { |
|
46
|
|
|
} |
|
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
|
|
|
->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
|
|
|
|