Skip to content

Commit 9922044

Browse files
author
Addin Gama Bertaqwa
committed
Add sign up event and listener
1 parent b369fb6 commit 9922044

File tree

9 files changed

+238
-6
lines changed

9 files changed

+238
-6
lines changed

app/Events/EmailCheck.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99
use Illuminate\Foundation\Events\Dispatchable;
1010
use Illuminate\Broadcasting\InteractsWithSockets;
1111
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
use Illuminate\Support\Facades\Log;
1213

1314
class EmailCheck
1415
{
1516
use Dispatchable, InteractsWithSockets, SerializesModels;
1617

1718
public $email;
19+
public $password;
20+
public $fullname;
21+
public $phone;
1822

1923
/**
2024
* Create a new event instance.
21-
*
22-
* @return void
25+
* @param $email
26+
* @param string $password
27+
* @param string $fullname
28+
* @param string $phone
2329
*/
24-
public function __construct($email)
30+
public function __construct($email, $password = '', $fullname = '', $phone = '')
2531
{
2632
$this->email = $email;
33+
$this->password = $password;
34+
$this->fullname = $fullname;
35+
$this->phone = $phone;
36+
37+
Log::info('[EVENT] EmailCheck');
2738
}
2839

2940
/**

app/Events/SignIn.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class SignIn
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
public $email;
18+
public $password;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct($email, $password)
26+
{
27+
$this->email = $email;
28+
$this->password = $password;
29+
}
30+
31+
/**
32+
* Get the channels the event should broadcast on.
33+
*
34+
* @return Channel|array
35+
*/
36+
public function broadcastOn()
37+
{
38+
return new PrivateChannel('channel-name');
39+
}
40+
}

app/Events/SignUp.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
use Illuminate\Support\Facades\Log;
13+
14+
class SignUp
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
public $email;
19+
public $password;
20+
public $fullname;
21+
public $phone;
22+
23+
/**
24+
* Create a new event instance.
25+
* @param $email
26+
* @param string $password
27+
* @param string $fullname
28+
* @param string $phone
29+
*/
30+
public function __construct($email, $password = '', $fullname = '', $phone = '')
31+
{
32+
$this->email = $email;
33+
$this->password = $password;
34+
$this->fullname = $fullname;
35+
$this->phone = $phone;
36+
37+
Log::info('[EVENT] SignUp');
38+
}
39+
40+
/**
41+
* Get the channels the event should broadcast on.
42+
*
43+
* @return Channel|array
44+
*/
45+
public function broadcastOn()
46+
{
47+
return new PrivateChannel('channel-name');
48+
}
49+
}

app/Http/Controllers/Auth/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public function __construct()
4747
*/
4848
public function authenticated(Request $request, $user)
4949
{
50-
event(new EmailCheck($user->email));
50+
event(new EmailCheck($request['email'], $request['password'], $user->name, $user->phone));
5151
}
5252
}

app/Http/helpers.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,39 @@ function emailCheck($email) {
3636
'apiKey' => Config::get('tmoney.api_key')
3737
]
3838
]);
39+
}
40+
41+
function signUp($accType, $email, $password, $fullname, $phone) {
42+
$client = new Client();
43+
return $client->post(Config::get('tmoney.base_url').'/sign-up', [
44+
'headers' => [
45+
'Authorization' => Config::get('tmoney.authorization'),
46+
'Accept' => 'application/json'
47+
],
48+
'form_params' => [
49+
'accType' => $accType,
50+
'userName' => $email,
51+
'password' => $password,
52+
'fullName' => $fullname,
53+
'phoneNo' => $phone,
54+
'terminal' => Config::get('tmoney.terminal'),
55+
'apiKey' => Config::get('tmoney.api_key')
56+
]
57+
]);
58+
}
59+
60+
function signIn($email, $password) {
61+
$client = new Client();
62+
return $client->post(Config::get('tmoney.base_url').'/sign-in', [
63+
'headers' => [
64+
'Authorization' => Config::get('tmoney.authorization'),
65+
'Accept' => 'application/json'
66+
],
67+
'form_params' => [
68+
'userName' => $email,
69+
'password' => $password,
70+
'terminal' => Config::get('tmoney.terminal'),
71+
'apiKey' => Config::get('tmoney.api_key')
72+
]
73+
]);
3974
}

app/Listeners/EmailCheckListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Listeners;
44

55
use App\Events\EmailCheck;
6+
use App\Events\SignUp;
67
use GuzzleHttp\Exception\BadResponseException;
78
use Illuminate\Queue\InteractsWithQueue;
89
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -29,12 +30,16 @@ public function __construct()
2930
public function handle(EmailCheck $event)
3031
{
3132
try {
33+
Log::info('[Listener] EmailCheckListener');
3234
$response = emailCheck($event->email);
3335
$body = $response->getBody();
3436
$json = json_decode($body);
3537
if ($json->resultCode == 188) {
3638
// TODO: dispatch signup event
37-
dd($json);
39+
event(new SignUp($event->email, $event->password, $event->fullname, $event->phone));
40+
} else {
41+
// TODO: dispatch login to TMoney
42+
3843
}
3944
} catch (BadResponseException $e) {
4045
Log::info($e->getMessage());

app/Listeners/SignInListener.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\SignIn;
6+
use Illuminate\Queue\InteractsWithQueue;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Support\Facades\Log;
9+
10+
class SignInListener
11+
{
12+
/**
13+
* Create the event listener.
14+
*
15+
* @return void
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Handle the event.
24+
*
25+
* @param SignIn $event
26+
* @return void
27+
*/
28+
public function handle(SignIn $event)
29+
{
30+
try {
31+
Log::info('[Listener] SignInListener');
32+
$response = signIn(
33+
$event->email,
34+
$event->password);
35+
$body = $response->getBody();
36+
$json = json_decode($body);
37+
// TODO: save idTmoney, idFusion to database
38+
dd($json);
39+
} catch (BadResponseException $e) {
40+
Log::info($e->getMessage());
41+
}
42+
}
43+
}

app/Listeners/SignUpListener.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\SignUp;
6+
use GuzzleHttp\Exception\BadResponseException;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Support\Facades\Log;
10+
11+
class SignUpListener
12+
{
13+
/**
14+
* Create the event listener.
15+
*
16+
* @return void
17+
*/
18+
public function __construct()
19+
{
20+
//
21+
}
22+
23+
/**
24+
* Handle the event.
25+
*
26+
* @param SignUp $event
27+
* @return void
28+
*/
29+
public function handle(SignUp $event)
30+
{
31+
try {
32+
Log::info('[Listener] SignUpListener');
33+
$response = signUp(1,
34+
$event->email,
35+
$event->password,
36+
$event->fullname,
37+
$event->phone);
38+
$body = $response->getBody();
39+
$json = json_decode($body);
40+
// TODO: save idTmoney, idFusion to database
41+
dd($json);
42+
} catch (BadResponseException $e) {
43+
Log::info($e->getMessage());
44+
}
45+
}
46+
}

app/Providers/EventServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class EventServiceProvider extends ServiceProvider
1818
],
1919
'App\Events\SignUp' => [
2020
'App\Listeners\SignUpListener'
21-
]
21+
],
22+
'App\Events\SignIn' => [
23+
'App\Listeners\SignInListener'
24+
],
2225
];
2326

2427
/**

0 commit comments

Comments
 (0)