Skip to content

Commit d90dc8f

Browse files
committed
fixed some code styles
1 parent 078338c commit d90dc8f

File tree

6 files changed

+111
-66
lines changed

6 files changed

+111
-66
lines changed

config/otp.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* ██████╗ ██╗ ██╗██████╗ ███╗ ███╗ ██████╗ ███╗ ██╗███████╗████████╗███████╗██████╗ ███████╗
45
* ██╔══██╗██║ ██║██╔══██╗ ████╗ ████║██╔═══██╗████╗ ██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗██╔════╝
@@ -10,7 +11,7 @@
1011
*/
1112
return [
1213

13-
/*
14+
/*
1415
* The format option allows you to decide
1516
* which generator implementation to be used when
1617
* generating new passwords.
@@ -30,25 +31,25 @@
3031

3132
'customize' => '123456789ABCDEFG@#$%',
3233

33-
/*
34+
/*
3435
* The length of the password.
3536
*/
3637

37-
'length' => env('OTP_LENGTH', 6),
38+
'length' => env('OTP_LENGTH', 6),
3839

3940
/*
4041
* The separator of the password.
4142
*/
4243

4344
'separator' => '-',
4445

45-
/*
46+
/*
4647
* Requiring correct input of uppercase and lowercase letters.
4748
*/
4849

49-
'sensitive' => env('OTP_SENSITIVE', false),
50+
'sensitive' => env('OTP_SENSITIVE', false),
5051

51-
/*
52+
/*
5253
* The expiry time of the password in minutes.
5354
*/
5455

@@ -60,7 +61,7 @@
6061

6162
'attempts' => env('OTP_ATTEMPT_TIMES', 5),
6263

63-
/*
64+
/*
6465
* The repeated password.
6566
* The previous password is valid when new password generated
6667
* until either one password used or itself expired.
@@ -75,7 +76,7 @@
7576

7677
'disposable' => true,
7778

78-
/*
79+
/*
7980
* The prefix of the cache key to be used to store.
8081
*/
8182

@@ -86,6 +87,6 @@
8687
*/
8788

8889
'demo' => env('OTP_DEMO', false),
89-
'demo_passwords' => ['1234','123456','12345678']
90+
'demo_passwords' => ['1234', '123456', '12345678'],
9091

91-
];
92+
];

resources/lang/en/messages.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
'invalid' => 'The :attribute is not a valid code.',
5-
'expired' => 'The :attribute is expired.',
6-
'max_attempt' => 'The :attribute reached the maximum allowed attempts.',
7-
];
4+
'invalid' => 'The :attribute is not a valid code.',
5+
'expired' => 'The :attribute is expired.',
6+
'max_attempt' => 'The :attribute reached the maximum allowed attempts.',
7+
];

src/Otp.php

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,150 @@
44

55
use Exception;
66
use Illuminate\Support\Facades\Cache;
7-
use Illuminate\Support\Facades\Facade;
87
use Illuminate\Support\Str;
9-
use Illuminate\Foundation\Application;
108

119
class Otp
1210
{
1311
private string $format = 'numeric';
12+
1413
private string $customize;
14+
1515
private int $length = 6;
16+
1617
private string $separator = '-';
18+
1719
private bool $sensitive = false;
20+
1821
private int $expires = 15;
22+
1923
private int $attempts = 5;
24+
2025
private bool $repeated = true;
26+
2127
private bool $disposable = true;
28+
2229
private string $prefix = 'OTPPX_';
30+
2331
private $data;
32+
2433
private bool $skip = false;
34+
2535
private bool $demo = false;
36+
2637
private array $demo_passwords = ['1234', '123456', '12345678'];
2738

2839
public function __construct()
2940
{
3041
foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'attempts', 'repeated', 'disposable', 'prefix', 'data', 'demo', 'demo_passwords'] as $value) {
31-
if (!empty(config('otp.' . $value))) $this->{$value} = config('otp.' . $value);
42+
if (! empty(config('otp.'.$value))) {
43+
$this->{$value} = config('otp.'.$value);
44+
}
3245
}
3346
}
3447

3548
public function __call(string $method, $params)
3649
{
37-
if (!str_starts_with($method, 'set')) {
50+
if (! str_starts_with($method, 'set')) {
3851
return;
3952
}
4053

4154
$property = Str::camel(substr($method, 3));
42-
if (!property_exists($this, $property)) {
55+
if (! property_exists($this, $property)) {
4356
return;
4457
}
4558

4659
$this->{$property} = $params[0] ?? null;
4760
if ($property == 'customize') {
4861
$this->format = 'customize';
4962
}
63+
5064
return $this;
5165
}
5266

5367
/**
5468
* @throws Exception
5569
*/
56-
public function generate(string $identifier = null, array $options = []): string
70+
public function generate(?string $identifier = null, array $options = []): string
5771
{
58-
if (!empty($options)) foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'repeated', 'prefix', 'data'] as $value) {
59-
if (isset($options[$value])) $this->{$value} = $options[$value];
72+
if (! empty($options)) {
73+
foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'repeated', 'prefix', 'data'] as $value) {
74+
if (isset($options[$value])) {
75+
$this->{$value} = $options[$value];
76+
}
77+
}
6078
}
6179

62-
if ($identifier === null) $identifier = session()->getId();
80+
if ($identifier === null) {
81+
$identifier = session()->getId();
82+
}
6383
$array = $this->repeated ? $this->readData($identifier, []) : [];
6484
$password = $this->generateNewPassword();
65-
if (!$this->sensitive) $password = strtoupper($password);
85+
if (! $this->sensitive) {
86+
$password = strtoupper($password);
87+
}
6688
$array[md5($password)] = [
6789
'expires' => time() + $this->expires * 60,
68-
'data' => $this->data
90+
'data' => $this->data,
6991
];
7092
$this->writeData($identifier, $array);
93+
7194
return $password;
7295
}
7396

74-
public function validate(string $identifier = null, string $password = null, array $options = []): object
97+
public function validate(?string $identifier = null, ?string $password = null, array $options = []): object
7598
{
76-
if (!empty($options)) foreach (['attempts', 'sensitive', 'disposable', 'skip'] as $value) {
77-
if (isset($options[$value])) $this->{$value} = $options[$value];
99+
if (! empty($options)) {
100+
foreach (['attempts', 'sensitive', 'disposable', 'skip'] as $value) {
101+
if (isset($options[$value])) {
102+
$this->{$value} = $options[$value];
103+
}
104+
}
78105
}
79106

80107
if ($password === null) {
81108
if ($identifier === null) {
82-
throw new Exception("Validate parameter can not be null");
109+
throw new Exception('Validate parameter can not be null');
83110
}
84111
$password = $identifier;
85112
$identifier = null;
86113
}
87114

88115
if ($this->demo && in_array($password, $this->demo_passwords)) {
89-
return (object)[
116+
return (object) [
90117
'status' => true,
91-
'demo' => true
118+
'demo' => true,
92119
];
93120
}
94121

95-
if ($identifier === null) $identifier = session()->getId();
96-
$attempt = $this->readData('_attempt_' . $identifier, 0);
122+
if ($identifier === null) {
123+
$identifier = session()->getId();
124+
}
125+
$attempt = $this->readData('_attempt_'.$identifier, 0);
97126
if ($attempt >= $this->attempts) {
98-
return (object)[
127+
return (object) [
99128
'status' => false,
100129
'error' => 'max_attempt',
101130
];
102131
}
103132

104133
$codes = $this->readData($identifier, []);
105-
if (!$this->sensitive) $password = strtoupper($password);
134+
if (! $this->sensitive) {
135+
$password = strtoupper($password);
136+
}
137+
138+
if (! isset($codes[md5($password)])) {
139+
$this->writeData('_attempt_'.$identifier, $attempt + 1);
106140

107-
if (!isset($codes[md5($password)])) {
108-
$this->writeData('_attempt_' . $identifier, $attempt + 1);
109-
return (object)[
141+
return (object) [
110142
'status' => false,
111143
'error' => 'invalid',
112144
];
113145
}
114146

115147
if (time() > $codes[md5($password)]['expires']) {
116-
$this->writeData('_attempt_' . $identifier, $attempt + 1);
117-
return (object)[
148+
$this->writeData('_attempt_'.$identifier, $attempt + 1);
149+
150+
return (object) [
118151
'status' => false,
119152
'error' => 'expired',
120153
];
@@ -124,37 +157,46 @@ public function validate(string $identifier = null, string $password = null, arr
124157
'status' => true,
125158
];
126159

127-
if (!empty($codes[md5($password)]['data'])) {
160+
if (! empty($codes[md5($password)]['data'])) {
128161
$response['data'] = $codes[md5($password)]['data'];
129162
}
130163

131-
if (!$this->skip) $this->forget($identifier, !$this->disposable ? $password : null);
164+
if (! $this->skip) {
165+
$this->forget($identifier, ! $this->disposable ? $password : null);
166+
}
132167
$this->resetAttempt($identifier);
133168

134-
return (object)$response;
169+
return (object) $response;
135170
}
136171

137-
public function forget(string $identifier = null, string $password = null): bool
172+
public function forget(?string $identifier = null, ?string $password = null): bool
138173
{
139-
if ($identifier === null) $identifier = session()->getId();
174+
if ($identifier === null) {
175+
$identifier = session()->getId();
176+
}
140177
if ($password !== null) {
141178
$codes = $this->readData($identifier, []);
142-
if (!isset($codes[md5($password)])) {
179+
if (! isset($codes[md5($password)])) {
143180
return false;
144181
}
145182
unset($codes[md5($password)]);
146183
$this->writeData($identifier, $codes);
184+
147185
return true;
148186
}
149187

150188
$this->deleteData($identifier);
189+
151190
return true;
152191
}
153192

154-
public function resetAttempt(string $identifier = null): bool
193+
public function resetAttempt(?string $identifier = null): bool
155194
{
156-
if ($identifier === null) $identifier = session()->getId();
157-
$this->deleteData('_attempt_' . $identifier);
195+
if ($identifier === null) {
196+
$identifier = session()->getId();
197+
}
198+
$this->deleteData('_attempt_'.$identifier);
199+
158200
return true;
159201
}
160202

@@ -168,7 +210,7 @@ private function generateNewPassword(): string
168210
'string' => $this->sensitive ? '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ' : '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',
169211
'numeric' => '0123456789',
170212
'numeric-no-zero' => '123456789',
171-
'customize' => $this->customize
213+
'customize' => $this->customize,
172214
];
173215

174216
$lengths = is_array($this->length) ? $this->length : [$this->length];
@@ -180,22 +222,22 @@ private function generateNewPassword(): string
180222

181223
return implode($this->separator, $password);
182224
} catch (Exception) {
183-
throw new Exception("Fail to generate password, please check the format is correct.");
225+
throw new Exception('Fail to generate password, please check the format is correct.');
184226
}
185227
}
186228

187229
private function writeData(string $key, mixed $value): void
188230
{
189-
Cache::put($this->prefix . $key, $value, $this->expires * 60 * 3);
231+
Cache::put($this->prefix.$key, $value, $this->expires * 60 * 3);
190232
}
191233

192234
private function readData(string $key, mixed $default = null): mixed
193235
{
194-
return Cache::get($this->prefix . $key, $default);
236+
return Cache::get($this->prefix.$key, $default);
195237
}
196238

197239
private function deleteData(string $key): void
198240
{
199-
Cache::forget($this->prefix . $key);
241+
Cache::forget($this->prefix.$key);
200242
}
201-
}
243+
}

src/OtpFacade.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ class OtpFacade extends Facade
88
{
99
/**
1010
* Get the registered name of the component.
11-
*
12-
* @return string
1311
*/
1412
protected static function getFacadeAccessor(): string
1513
{
1614
return 'otp';
1715
}
18-
}
16+
}

src/OtpServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace PhpMonsters\Otp;
44

5-
use Illuminate\Support\ServiceProvider;
65
use Illuminate\Foundation\Application;
6+
use Illuminate\Support\ServiceProvider;
77

88
class OtpServiceProvider extends ServiceProvider
99
{
@@ -25,10 +25,10 @@ public function register()
2525
public function boot()
2626
{
2727
$this->loadTranslationsFrom(__DIR__.'/../lang', 'otp');
28-
28+
2929
$this->publishes([
3030
__DIR__.'/../resources/lang' => $this->app->langPath('vendor/otp'),
31-
__DIR__.'/../config/otp.php' => config_path('otp.php')
31+
__DIR__.'/../config/otp.php' => config_path('otp.php'),
3232
]);
3333
}
34-
}
34+
}

0 commit comments

Comments
 (0)