EmailRequestFinder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 45
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findEmailAmountInSetTimespan() 0 8 1
A findLastEmailRequestTimestamp() 0 8 2
1
<?php
2
3
namespace App\Module\Security\Email\Service;
4
5
use App\Infrastructure\Settings\Settings;
6
use App\Module\Security\Email\Repository\EmailLogFinderRepository;
7
8
class EmailRequestFinder
9
{
10
    private array $securitySettings;
11
12 29
    public function __construct(
13
        private readonly EmailLogFinderRepository $emailRequestFinderRepository,
14
        Settings $settings,
15
    ) {
16 29
        $this->securitySettings = $settings->get('security');
17
    }
18
19
    /**
20
     * Retrieve email requests from given email or user.
21
     *
22
     * @param string $email
23
     * @param int|null $userId
24
     *
25
     * @return int
26
     */
27 15
    public function findEmailAmountInSetTimespan(string $email, ?int $userId): int
28
    {
29
        // This service should be called when retrieving ip stats as this class loads the settings it
30
        // Stats concerning given email in last timespan
31 15
        return $this->emailRequestFinderRepository->getLoggedEmailCountInTimespan(
32 15
            $email,
33 15
            $this->securitySettings['timespan'],
34 15
            $userId
35 15
        );
36
    }
37
38
    /**
39
     * Returns the very last EMAIL request from actual ip or given email.
40
     *
41
     * @param string $email
42
     *
43
     * @return int
44
     */
45 6
    public function findLastEmailRequestTimestamp(string $email): int
46
    {
47 6
        $createdAt = $this->emailRequestFinderRepository->findLatestEmailRequest($email);
48 6
        if (is_string($createdAt)) {
49 6
            return (int)(new \DateTime($createdAt))->format('U');
50
        }
51
52
        return 0;
53
    }
54
}
55