Skip to content

Commit 4d5f2cf

Browse files
committed
Refactoring
1 parent 1aa0cba commit 4d5f2cf

7 files changed

+51
-52
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
}
3535
},
3636
"require": {
37-
"php":"^8.0",
38-
"alexgeno/phone-verification": "^1.0"
37+
"php": "^8.0",
38+
"alexgeno/phone-verification": "^1.0",
39+
"laravel/framework": "^9.0"
3940
},
4041
"suggest": {
4142
"predis/predis": "required to use Redis as a storage",

src/Http/Controllers/PhoneVerificationController.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AlexGeno\PhoneVerification\Manager\Completer;
77
use AlexGeno\PhoneVerification\Manager\Initiator;
88
use Illuminate\Http\Request;
9+
use Illuminate\Http\Response;
910
use Illuminate\Routing\Controller;
1011

1112
class PhoneVerificationController extends Controller
@@ -18,12 +19,12 @@ class PhoneVerificationController extends Controller
1819
public function initiate(Initiator $manager, Request $request)
1920
{
2021
$response = ['ok' => true, 'message' => trans('phone-verification::messages.initiation_success')];
21-
$status = 200;
22+
$status = Response::HTTP_OK;
2223
try {
2324
$manager->initiate($request->post('to'));
2425
} catch (Exception $e) {
2526
$response = ['ok' => false, 'message' => $e->getMessage()];
26-
$status = 406;
27+
$status = Response::HTTP_NOT_ACCEPTABLE;
2728
}
2829

2930
return response()->json($response, $status);
@@ -37,12 +38,12 @@ public function initiate(Initiator $manager, Request $request)
3738
public function complete(Completer $manager, Request $request)
3839
{
3940
$response = ['ok' => true, 'message' => trans('phone-verification::messages.completion_success')];
40-
$status = 200;
41+
$status = Response::HTTP_OK;
4142
try {
4243
$manager->complete($request->post('to'), (int) $request->post('otp'));
4344
} catch (Exception $e) {
4445
$response = ['ok' => false, 'message' => $e->getMessage()];
45-
$status = 406;
46+
$status = Response::HTTP_NOT_ACCEPTABLE;
4647
}
4748

4849
return response()->json($response, $status);

src/Notifications/Otp.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ class Otp extends Notification
99
{
1010
protected string $content;
1111

12-
protected string $channel;
13-
14-
public function __construct(string $channel)
15-
{
16-
$this->channel = $channel;
12+
public function __construct(
13+
protected string $channel
14+
) {
1715
}
1816

1917
/**

src/PhoneVerificationServiceProvider.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use AlexGeno\PhoneVerification\Manager\Initiator;
88
use AlexGeno\PhoneVerification\Sender\I as ISender;
99
use AlexGeno\PhoneVerification\Storage\I as IStorage;
10+
use AlexGeno\PhoneVerification\Storage\MongoDb;
11+
use AlexGeno\PhoneVerification\Storage\Redis;
1012
use AlexGeno\PhoneVerificationLaravel\Commands\Complete;
1113
use AlexGeno\PhoneVerificationLaravel\Commands\Initiate;
1214
use AlexGeno\PhoneVerificationLaravel\Notifications\Otp;
13-
use Illuminate\Support\Facades\DB;
14-
use Illuminate\Support\Facades\Redis;
15+
use Illuminate\Contracts\Foundation\Application;
1516
use Illuminate\Support\ServiceProvider;
1617

1718
class PhoneVerificationServiceProvider extends ServiceProvider
@@ -21,12 +22,12 @@ class PhoneVerificationServiceProvider extends ServiceProvider
2122
*/
2223
public function register()
2324
{
24-
$this->registerConfig();
2525
$this->registerStorage();
2626
$this->registerSender();
2727
$this->registerOtp();
2828
$this->registerManager();
2929
$this->registerPhoneVerification();
30+
$this->registerConfig();
3031
}
3132

3233
/**
@@ -36,7 +37,7 @@ public function register()
3637
*/
3738
protected function registerPhoneVerification()
3839
{
39-
$this->app->singleton(PhoneVerification::class, function ($container) {
40+
$this->app->singleton(PhoneVerification::class, function (Application $container) {
4041
return new PhoneVerification();
4142
});
4243
}
@@ -45,30 +46,28 @@ protected function registerPhoneVerification()
4546
* Return the Redis storage instance.
4647
*
4748
* @param array<mixed> $config ['settings' => [...], 'connection' => string]
48-
* @return \AlexGeno\PhoneVerification\Storage\Redis
4949
*/
50-
protected function redisStorage(array $config)
50+
protected function redisStorage(array $config): Redis
5151
{
52-
$connection = Redis::connection($config['connection']);
52+
$connection = $this->app->make('redis')->connection($config['connection']);
5353

54-
return new \AlexGeno\PhoneVerification\Storage\Redis($connection->client(), $config['settings']);
54+
return new Redis($connection->client(), $config['settings']);
5555
}
5656

5757
/**
5858
* Return the Mongodb storage instance.
5959
*
6060
* @param array<mixed> $config ['settings' => [...], 'connection' => string]
61-
* @return \AlexGeno\PhoneVerification\Storage\MongoDb
6261
*/
63-
protected function mongodbStorage(array $config)
62+
protected function mongodbStorage(array $config): MongoDb
6463
{
6564
/**
6665
* @var \Jenssegers\Mongodb\Connection
6766
*/
68-
$connection = DB::connection($config['connection']);
67+
$connection = $this->app->make('db')->connection($config['connection']);
6968
$config['settings']['db'] = $connection->getDatabaseName();
7069

71-
return new \AlexGeno\PhoneVerification\Storage\MongoDb($connection->getMongoClient(), $config);
70+
return new MongoDb($connection->getMongoClient(), $config);
7271
}
7372

7473
/**
@@ -80,7 +79,7 @@ protected function mongodbStorage(array $config)
8079
*/
8180
protected function registerStorage()
8281
{
83-
$this->app->bind(IStorage::class, function ($container) {
82+
$this->app->bind(IStorage::class, function (Application $container) {
8483
$config = config('phone-verification.storage');
8584
$driver = $config['driver'];
8685
$method = $driver.'Storage';
@@ -104,7 +103,7 @@ protected function registerSender()
104103
{
105104
$this->app->when(Sender::class)->needs('$driver')->giveConfig('phone-verification.sender.driver');
106105
$this->app->when(Sender::class)->needs('$toLog')->giveConfig('phone-verification.sender.to_log');
107-
$this->app->bind(ISender::class, function ($container) {
106+
$this->app->bind(ISender::class, function (Application $container) {
108107
return $container->make(Sender::class);
109108
});
110109
}
@@ -126,7 +125,7 @@ protected function registerOtp()
126125
*
127126
* @return array<mixed>
128127
*/
129-
protected function managerConfig()
128+
protected function managerConfig(): array
130129
{
131130
$config = config('phone-verification.manager');
132131
// load translated messages
@@ -152,12 +151,12 @@ protected function managerConfig()
152151
protected function registerManager()
153152
{
154153
// a sender and a storage for initiation process
155-
$this->app->bind(Initiator::class, function ($container) {
154+
$this->app->bind(Initiator::class, function (Application $container) {
156155
return (new Manager($container->make(IStorage::class), $this->managerConfig()))
157156
->sender($container->make(ISender::class));
158157
});
159158
// only a storage for completion process
160-
$this->app->bind(Completer::class, function ($container) {
159+
$this->app->bind(Completer::class, function (Application $container) {
161160
return new Manager($container->make(IStorage::class), $this->managerConfig());
162161
});
163162
}

src/Sender.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@
44

55
use AlexGeno\PhoneVerification\Sender\I;
66
use AlexGeno\PhoneVerificationLaravel\Notifications\Otp;
7-
use Illuminate\Support\Facades\Notification;
7+
use Illuminate\Notifications\AnonymousNotifiable;
8+
use Psr\Log\LoggerInterface;
89

910
class Sender implements I
1011
{
11-
protected Otp $otp;
12-
13-
protected string $driver;
14-
15-
protected bool $toLog;
16-
1712
/**
1813
* Constructor.
1914
*/
20-
public function __construct(Otp $otp, string $driver, bool $toLog)
21-
{
22-
$this->otp = $otp;
23-
$this->driver = $driver;
24-
$this->toLog = $toLog;
15+
public function __construct(
16+
protected Otp $otp,
17+
protected AnonymousNotifiable $notifiable,
18+
protected LoggerInterface $logger,
19+
protected string $driver,
20+
protected bool $toLog
21+
) {
2522
}
2623

2724
/**
@@ -32,9 +29,9 @@ public function __construct(Otp $otp, string $driver, bool $toLog)
3229
public function invoke(string $to, string $text)
3330
{
3431
if ($this->toLog) {
35-
\Illuminate\Support\Facades\Log::info("Pretended notification sending to {$this->driver}:{$to} with message: {$text}");
32+
$this->logger->info("Pretended notification sending to {$this->driver}:{$to} with message: {$text}");
3633
} else {
37-
Notification::route($this->driver, $to)->notify($this->otp->content($text));
34+
$this->notifiable->route($this->driver, $to)->notify($this->otp->content($text));
3835
}
3936
}
4037
}

tests/Feature/IgnoreRoutesTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AlexGeno\PhoneVerificationLaravel\Tests\Feature;
44

5+
use Illuminate\Http\Response;
6+
57
class IgnoreRoutesTest extends FeatureTestCase
68
{
79
/**
@@ -21,7 +23,7 @@ protected function getEnvironmentSetUp($app)
2123
public function test_initiation_not_available()
2224
{
2325
$response = $this->postJson('/phone-verification/initiate', ['to' => '+15417543010']);
24-
$response->assertStatus(404);
26+
$response->assertStatus(Response::HTTP_NOT_FOUND);
2527
}
2628

2729
/**
@@ -32,6 +34,6 @@ public function test_initiation_not_available()
3234
public function test_completion_not_available()
3335
{
3436
$response = $this->postJson('/phone-verification/complete', ['to' => '+15417543010', 'otp' => 1234]);
35-
$response->assertStatus(404);
37+
$response->assertStatus(Response::HTTP_NOT_FOUND);
3638
}
3739
}

tests/Feature/UseRoutesTest.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AlexGeno\PhoneVerificationLaravel\Tests\Feature;
44

5+
use Illuminate\Http\Response;
56
use phpmock\phpunit\PHPMock;
67

78
class UseRoutesTest extends FeatureTestCase
@@ -19,7 +20,7 @@ public function test_initiation_ok()
1920
{
2021
$response = $this->postJson('/phone-verification/initiate', ['to' => '+15417543010']);
2122

22-
$response->assertStatus(200);
23+
$response->assertStatus(Response::HTTP_OK);
2324

2425
$response->assertJson(['ok' => true, 'message' => trans(self::LANG_MESSAGES.'initiation_success')]);
2526
}
@@ -34,7 +35,7 @@ public function test_initiation_rate_limit_exceeded()
3435
config(['phone-verification.manager.rate_limits.initiate' => ['count' => 0, 'period_secs' => 3600]]);
3536
$response = $this->postJson('/phone-verification/initiate', ['to' => '+15417543010']);
3637

37-
$response->assertStatus(406);
38+
$response->assertStatus(Response::HTTP_NOT_ACCEPTABLE);
3839

3940
$response->assertJson(['ok' => false, 'message' => trans(self::LANG_MESSAGES.'initiation_rate_limit', ['sms' => 0, 'hours' => 1])]);
4041
}
@@ -60,11 +61,11 @@ public function test_process_ok()
6061
$rand->expects($this->once())->willReturn($otp);
6162

6263
$response = $this->postJson('/phone-verification/initiate', ['to' => $to]);
63-
$response->assertStatus(200);
64+
$response->assertStatus(Response::HTTP_OK);
6465
$response->assertJson(['ok' => true, 'message' => trans(self::LANG_MESSAGES.'initiation_success')]);
6566

6667
$response = $this->postJson('/phone-verification/complete', ['to' => $to, 'otp' => $otp]);
67-
$response->assertStatus(200);
68+
$response->assertStatus(Response::HTTP_OK);
6869
$response->assertJson(['ok' => true, 'message' => trans(self::LANG_MESSAGES.'completion_success')]);
6970
}
7071

@@ -78,7 +79,7 @@ public function test_process_rate_limit_exceeded()
7879
config(['phone-verification.manager.rate_limits.complete' => ['count' => 0, 'period_secs' => 60]]);
7980
$response = $this->postJson('/phone-verification/complete', ['to' => '+15417543010', 'otp' => 0]);
8081

81-
$response->assertStatus(406);
82+
$response->assertStatus(Response::HTTP_NOT_ACCEPTABLE);
8283

8384
$response->assertJson(['ok' => false, 'message' => trans(self::LANG_MESSAGES.'completion_rate_limit', ['times' => 0, 'minutes' => 1])]);
8485
}
@@ -92,13 +93,13 @@ public function test_completion_otp_incorrect()
9293
{
9394
$response = $this->postJson('/phone-verification/initiate', ['to' => '+15417543010']);
9495

95-
$response->assertStatus(200);
96+
$response->assertStatus(Response::HTTP_OK);
9697

9798
$response->assertJson(['ok' => true, 'message' => trans(self::LANG_MESSAGES.'initiation_success')]);
9899

99100
$response = $this->postJson('/phone-verification/complete', ['to' => '+15417543010', 'otp' => 0]);
100101

101-
$response->assertStatus(406);
102+
$response->assertStatus(Response::HTTP_NOT_ACCEPTABLE);
102103

103104
$response->assertJson(['ok' => false, 'message' => trans(self::LANG_MESSAGES.'incorrect')]);
104105
}
@@ -115,7 +116,7 @@ public function test_completion_otp_expired()
115116

116117
$response = $this->postJson('/phone-verification/complete', ['to' => '+15417543010', 'otp' => 0]);
117118

118-
$response->assertStatus(406);
119+
$response->assertStatus(Response::HTTP_NOT_ACCEPTABLE);
119120

120121
$response->assertJson(['ok' => false, 'message' => trans(self::LANG_MESSAGES.'expired', ['minutes' => $expirationPeriodSecs / 60])]);
121122
}

0 commit comments

Comments
 (0)