• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 22540946991

01 Mar 2026 09:55AM UTC coverage: 63.267%. Remained the same
22540946991

push

github

web-flow
Merge pull request #37 from daycry/development

Refactor Auth configuration and update CHANGELOG for v4.0.0

32 of 42 new or added lines in 18 files covered. (76.19%)

1 existing line in 1 file now uncovered.

3064 of 4843 relevant lines covered (63.27%)

41.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.94
/src/Services/AttemptHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Services;
15

16
use CodeIgniter\HTTP\RequestInterface;
17
use CodeIgniter\HTTP\ResponseInterface;
18
use Daycry\Auth\Models\AttemptModel;
19
use Daycry\Auth\Validators\AttemptValidator;
20

21
/**
22
 * Service for handling invalid login attempts
23
 *
24
 * Extracts attempt handling logic from BaseControllerTrait
25
 */
26
class AttemptHandler
27
{
28
    protected AttemptModel $attemptModel;
29
    protected bool $isEnabled;
30

31
    public function __construct()
37✔
32
    {
33
        $this->attemptModel = new AttemptModel();
37✔
34
        $this->isEnabled    = service('settings')->get('AuthSecurity.enableInvalidAttempts') === true;
37✔
35
    }
36

37
    /**
38
     * Check if attempts validation is enabled and validate if needed
39
     */
40
    public function validateAttempts(ResponseInterface $response): void
2✔
41
    {
42
        if (! $this->isEnabled) {
2✔
43
            return;
2✔
44
        }
45

46
        AttemptValidator::check($response);
×
47
    }
48

49
    /**
50
     * Handle invalid attempts by updating the attempts counter
51
     */
52
    public function handleInvalidAttempt(RequestInterface $request): void
5✔
53
    {
54
        if (! $this->isEnabled) {
5✔
55
            return;
5✔
56
        }
57

58
        $ipAddress = $request->getIPAddress();
×
59
        $attempt   = $this->attemptModel->where('ip_address', $ipAddress)->first();
×
60

61
        if ($attempt === null) {
×
62
            $this->createNewAttempt($ipAddress);
×
NEW
63
        } elseif ($attempt->attempts < service('settings')->get('AuthSecurity.maxAttempts')) {
×
64
            $this->incrementAttempt($attempt);
×
65
        }
66
    }
67

68
    /**
69
     * Create a new attempt record
70
     */
71
    private function createNewAttempt(string $ipAddress): void
×
72
    {
73
        $attempt = [
×
74
            'user_id'      => auth()->user()?->id,
×
75
            'ip_address'   => $ipAddress,
×
76
            'attempts'     => 1,
×
77
            'hour_started' => time(),
×
78
        ];
×
79

80
        $this->attemptModel->save($attempt);
×
81
    }
82

83
    /**
84
     * Increment existing attempt counter
85
     */
86
    private function incrementAttempt(object $attempt): void
×
87
    {
88
        $attempt->attempts++;
×
89
        $this->attemptModel->save($attempt);
×
90
    }
91

92
    /**
93
     * Check if attempts handling is enabled
94
     */
95
    public function isEnabled(): bool
3✔
96
    {
97
        return $this->isEnabled;
3✔
98
    }
99

100
    /**
101
     * Static method to get a new instance
102
     */
103
    public static function getInstance(): self
2✔
104
    {
105
        return new self();
2✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc