Completed
Push — development ( 96664a...f2a74e )
by Claudio
02:38
created

Mail::getByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Models\Mail as MailModel;
6
use App\Singleton;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Mail as MailFacade;
9
10
/**
11
 * Class Mail.
12
 */
13
final class Mail extends Singleton
14
{
15
    /**
16
     * Stored Mail Model.
17
     *
18
     * @var MailModel
19
     */
20
    protected $mailModel;
21
22
    /**
23
     * Send an Email.
24
     *
25
     * @param array $configuration
26
     * @param string $view
27
     */
28
    public function send(array $configuration, string $view = 'habbo-web-mail.confirm-mail')
29
    {
30
        if (Config::get('mail.enable')) {
31
            MailFacade::send($view, $configuration, function ($message) use ($configuration) {
32
                $message->from(Config::get('mail.from.address'), Config::get('mail.from.name'));
33
                $message->to($configuration['email'])->subject($configuration['subject']);
34
            });
35
        }
36
    }
37
38
    /**
39
     * Store an E-mail.
40
     *
41
     * @param string $email
42
     * @param string $url
43
     *
44
     * @return string
45
     */
46
    public function store(string $email, string $url): string
47
    {
48
        (new MailModel())->store($token = uniqid('HabboMail', true), $url, $email);
49
50
        return $token;
51
    }
52
53
    /**
54
     * Return if Exists E-Mail with that Token.
55
     *
56
     * @param string $token
57
     *
58
     * @return bool
59
     */
60
    public function has(string $token)
61
    {
62
        return $this->get($token) !== null;
63
    }
64
65
    /**
66
     * Get an E-mail by Token.
67
     *
68
     * @param string $token
69
     *
70
     * @return MailModel
71
     */
72
    public function get(string $token = '')
73
    {
74
        if ($this->mailModel == null && !empty($token)) {
75
            $mailModel = MailModel::where('token', $token)->where('used', '0')->first();
76
77
            if ($mailModel !== null) {
78
                $this->set($mailModel);
79
80
                $this->update(['used' => '1']);
81
            }
82
        }
83
84
        return $this->mailModel;
85
    }
86
87
    /**
88
     * Set Mail Model in Cache.
89
     *
90
     * @param MailModel $model
91
     *
92
     * @return MailModel
93
     */
94
    public function set(MailModel $model)
95
    {
96
        return $this->mailModel = $model;
97
    }
98
99
    /**
100
     * Update Mail Model Data.
101
     *
102
     * @param array $parameters
103
     *
104
     * @return MailModel
105
     */
106
    public function update(array $parameters)
107
    {
108
        $this->mailModel->update($parameters);
109
110
        return $this->mailModel;
111
    }
112
}
113