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

daycry / auth / 23386290410

21 Mar 2026 06:48PM UTC coverage: 64.989% (+1.2%) from 63.76%
23386290410

push

github

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

v5.0.0

302 of 465 new or added lines in 26 files covered. (64.95%)

19 existing lines in 3 files now uncovered.

3306 of 5087 relevant lines covered (64.99%)

47.04 hits per line

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

0.0
/src/Libraries/TokenEmailSender.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\Libraries;
15

16
use CodeIgniter\Exceptions\RuntimeException;
17
use CodeIgniter\HTTP\IncomingRequest;
18
use CodeIgniter\I18n\Time;
19
use Daycry\Auth\Entities\User;
20
use Daycry\Auth\Models\UserIdentityModel;
21
use Daycry\Auth\Traits\Viewable;
22

23
/**
24
 * Generates a cryptographic token, stores it as an identity, and sends
25
 * an email with the token to the user.
26
 *
27
 * Shared by MagicLinkController and PasswordResetController.
28
 */
29
class TokenEmailSender
30
{
31
    use Viewable;
32

33
    /**
34
     * Generates a token identity and sends an email to the user.
35
     *
36
     * @param User                 $user          The user to send the email to
37
     * @param string               $identityType  Identity type constant (e.g. Session::ID_TYPE_MAGIC_LINK)
38
     * @param int                  $lifetime      Token lifetime in seconds
39
     * @param string               $emailSubject  Email subject line (lang key already resolved)
40
     * @param string               $emailView     View path for the email body
41
     * @param array<string, mixed> $extraViewData Additional data passed to the email view
42
     *
43
     * @return string The generated raw token
44
     *
45
     * @throws RuntimeException When the email cannot be sent
46
     */
NEW
47
    public function sendTokenEmail(
×
48
        User $user,
49
        string $identityType,
50
        int $lifetime,
51
        string $emailSubject,
52
        string $emailView,
53
        array $extraViewData = [],
54
    ): string {
55
        /** @var UserIdentityModel $identityModel */
NEW
56
        $identityModel = model(UserIdentityModel::class);
×
57

58
        // Delete any previous identities of this type
NEW
59
        $identityModel->deleteIdentitiesByType($user, $identityType);
×
60

61
        // Generate the token and save it as an identity
NEW
62
        helper('text');
×
NEW
63
        $token = random_string('crypto', 20);
×
64

NEW
65
        $identityModel->insert([
×
NEW
66
            'user_id' => $user->id,
×
NEW
67
            'type'    => $identityType,
×
NEW
68
            'secret'  => $token,
×
NEW
69
            'expires' => Time::now()->addSeconds($lifetime)->format('Y-m-d H:i:s'),
×
NEW
70
        ]);
×
71

72
        // Gather common email context
73
        /** @var IncomingRequest $request */
NEW
74
        $request   = service('request');
×
NEW
75
        $ipAddress = $request->getIPAddress();
×
NEW
76
        $userAgent = (string) $request->getUserAgent();
×
NEW
77
        $date      = Time::now()->toDateTimeString();
×
78

79
        // Build view data
NEW
80
        $viewData = array_merge([
×
NEW
81
            'token'     => $token,
×
NEW
82
            'ipAddress' => $ipAddress,
×
NEW
83
            'userAgent' => $userAgent,
×
NEW
84
            'date'      => $date,
×
NEW
85
        ], $extraViewData);
×
86

87
        // Send the email
NEW
88
        helper('email');
×
NEW
89
        $email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
×
NEW
90
        $email->setTo($user->email);
×
NEW
91
        $email->setSubject($emailSubject);
×
NEW
92
        $email->setMessage($this->view($emailView, $viewData));
×
93

NEW
94
        if ($email->send(false) === false) {
×
NEW
95
            log_message('error', $email->printDebugger(['headers']));
×
96

NEW
97
            $email->clear();
×
98

NEW
99
            throw new RuntimeException(
×
NEW
100
                'Cannot send email for user: ' . $user->email,
×
NEW
101
            );
×
102
        }
103

NEW
104
        $email->clear();
×
105

NEW
106
        return $token;
×
107
    }
108
}
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