Passed
Push — master ( 8ff4e3...ffee1f )
by Samuel
03:00
created

EmailLogFinderRepository::findLatestEmailRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Domain\Security\Repository;
4
5
use App\Infrastructure\Factory\QueryFactory;
0 ignored issues
show
Bug introduced by
The type App\Infrastructure\Factory\QueryFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
// Class cannot be readonly as it's mocked (doubled) in tests
8
class EmailLogFinderRepository
9
{
10 24
    public function __construct(
11
        private readonly QueryFactory $queryFactory
12
    ) {
13 24
    }
14
15
    /**
16
     * Retrieves request summary from the given ip address.
17
     *
18
     * @param string $email
19
     * @param int $seconds
20
     * Throws PersistenceRecordNotFoundException if entry not found
21
     * @param int|null $userId
22
     *
23
     * @return int
24
     */
25 10
    public function getLoggedEmailCountInTimespan(string $email, int $seconds, ?int $userId): int
26
    {
27 10
        $query = $this->queryFactory->selectQuery();
28 10
        $query->select(
29 10
            [
30 10
                'email_amount' => $query->func()->count('id'),
31 10
            ]
32 10
        )->from('email_log')->where(
33 10
            [
34
                // Return all between now and x number of minutes
35 10
                'created_at >' => $query->newExpr('DATE_SUB(NOW(), INTERVAL :sec SECOND)'),
36 10
            ]
37 10
        )->where( // Where to_email is $email or user_id is $userId if it's set
38 10
            $userId ? ['OR' => ['to_email' => $email], 'user_id' => $userId] : ['to_email' => $email]
39 10
        )->bind(':sec', $seconds, 'integer');
40
41
        // Only fetch and not fetchAll as result will be one row with the counts
42 10
        $result = $query->execute()->fetch('assoc');
43
44 10
        return (int)$result['email_amount'];
45
    }
46
47
    /**
48
     * Searches the latest email request concerning a specific email address.
49
     *
50
     * @param string $email
51
     *
52
     * @return string|bool datetime of last email request or false
53
     */
54 3
    public function findLatestEmailRequest(string $email): bool|string
55
    {
56 3
        $query = $this->queryFactory->selectQuery();
57 3
        $query->select('created_at')->from('email_log')->where(
58 3
            [
59 3
                'to_email' => $email,
60 3
            ]
61 3
        )->orderByDesc('created_at')->limit(1);
62
63 3
        return $query->execute()->fetch('assoc')['created_at'] ?: false;
64
    }
65
66
    /**
67
     * Gives sent email amount globally in the last given amount of days.
68
     *
69
     * @param int $days
70
     *
71
     * @return int sent email amount
72
     */
73 7
    public function getGlobalSentEmailAmount(int $days): int
74
    {
75 7
        $query = $this->queryFactory->selectQuery();
76 7
        $query->select(
77 7
            [
78 7
                'sent_email_amount' => $query->func()->count('id'),
79 7
            ]
80 7
        )->from('email_log')->where(
81 7
            [
82 7
                'created_at >' => $query->newExpr('DATE_SUB(NOW(), INTERVAL :days DAY)'),
83 7
            ]
84 7
        )->bind(':days', $days, 'integer');
85
86 7
        return (int)($query->execute()->fetch('assoc')['sent_email_amount'] ?? 0);
87
    }
88
}
89