Completed
Push — development ( b7987d...8c0e57 )
by Claudio
05:33
created

Mail   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 129
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMail() 0 4 1
A get() 0 4 1
A getInstance() 0 10 2
A send() 0 10 2
A store() 0 6 1
A update() 0 8 1
A has() 0 4 1
A getByToken() 0 8 1
A set() 0 4 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Models\Mail as MailModel;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\Mail as MailFacade;
8
9
/**
10
 * Class Mail
11
 * @package App\Helpers
12
 */
13
class Mail
14
{
15
    /**
16
     * Stored Mail Model
17
     *
18
     * @var MailModel|null
19
     */
20
    protected $mailModel = null;
21
22
    /**
23
     * Quick Way to get Cached MailModel
24
     *
25
     * @return MailModel|null
26
     */
27
    public function getMail()
28
    {
29
        return self::getInstance()->get();
30
    }
31
32
    /**
33
     * Return cached Mail Model
34
     *
35
     * @return MailModel|null
36
     */
37
    public function get()
38
    {
39
        return $this->mailModel;
40
    }
41
42
    /**
43
     * Create and return a Mail instance
44
     *
45
     * @return Mail
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = null;
50
51
        if ($instance === null) {
52
            $instance = new static();
53
        }
54
55
        return $instance;
56
    }
57
58
    /**
59
     * Send an Email
60
     *
61
     * @param array $configuration
62
     * @param string $view
63
     */
64
    public function send(array $configuration, string $view = 'habbo-web-mail.confirm-mail')
65
    {
66
        if (Config::get('mail.enable') == false)
67
            return;
68
69
        MailFacade::send($view, $configuration, function ($message) use ($configuration) {
70
            $message->from(Config::get('mail.from.address'), Config::get('mail.from.name'));
71
            $message->to($configuration['email'])->subject($configuration['subject']);
72
        });
73
    }
74
75
    /**
76
     * Store an E-mail
77
     *
78
     * @param string $email
79
     * @param string $url
80
     * @return string
81
     */
82
    public function store(string $email, string $url): string
83
    {
84
        (new MailModel)->store($token = uniqid('HabboMail', true), $url, $email)->save();
85
86
        return $token;
87
    }
88
89
    /**
90
     * Update Mail Model Data
91
     *
92
     * @param string $token
93
     * @param array $parameters
94
     * @return MailModel
95
     */
96
    public function update(string $token, array $parameters)
97
    {
98
        $mail = $this->get($token);
0 ignored issues
show
Unused Code introduced by
The call to Mail::get() has too many arguments starting with $token.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
99
100
        $mail->update($parameters);
101
102
        return $mail;
103
    }
104
105
    /**
106
     * Return if Exists E-Mail with that Token
107
     *
108
     * @param string $token
109
     * @return bool
110
     */
111
    public function has(string $token)
112
    {
113
        return Mail::getInstance()->getByToken($token) !== null;
114
    }
115
116
    /**
117
     * Get an E-mail by Token
118
     *
119
     * @param string $token
120
     * @return MailModel
121
     */
122
    public function getByToken(string $token)
123
    {
124
        $mailRequest = MailModel::where('token', $token)->where('used', '0')->first();
125
126
        $mailRequest->update(['used' => '1']);
127
128
        return $this->set($mailRequest);
129
    }
130
131
    /**
132
     * Set Mail Model in Cache
133
     *
134
     * @param MailModel $model
135
     * @return MailModel
136
     */
137
    public function set(MailModel $model)
138
    {
139
        return $this->mailModel = $model;
140
    }
141
}