EmailRequestFinder::findEmailAmountInSetTimespan()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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