Skip to content

Commit 86d396a

Browse files
authored
Merge pull request #72 from openclassify/register-event-end-trait-added
Registered Event Added Api Reponse Trait Added.
2 parents f81c08c + d020894 commit 86d396a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Http/Controller/ApiController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ public function register()
233233

234234
$user->setAttribute('activation_code', str_random(40));
235235
$user->save();
236+
event(new UserRegistered($user));
237+
236238

237239
$parameters['token'] = $encrypter->encrypt($user->getActivationCode());
238240
$parameters['success-verification'] = $encrypter->encrypt($this->request->get('success-params'));
@@ -242,6 +244,8 @@ public function register()
242244

243245
$url = url('api/register') . '?' . http_build_query($parameters);
244246

247+
//TODO::Unrelaible event. If one of the listeners throws an error, the flow is interrupted.
248+
// NEED ERROR HANDLING.
245249
event(new ActivateAccount($user, $url));
246250
// $user->notify(new ActivateYourAccount($url));
247251

src/Traits/ReturnResponseTrait.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Visiosoft\ConnectModule\Traits;
4+
5+
use Illuminate\Http\JsonResponse;
6+
7+
trait ReturnResponseTrait
8+
{
9+
protected array $output = [
10+
'success' => true,
11+
'data' => [],
12+
'message' => '',
13+
];
14+
15+
/**
16+
* @param $data
17+
* @param bool $success
18+
* @param string $message
19+
* @param int $statusCode
20+
* @return JsonResponse
21+
*/
22+
public function sendResponse($data = [], string $message = '', bool $success = true, int $statusCode = 200): JsonResponse
23+
{
24+
if (request()->method() == 'POST') {
25+
$statusCode = 201;
26+
}
27+
$this->output['success'] = $success;
28+
$this->output['data'] = $data;
29+
$this->output['message'] = $message;
30+
return $this->setResponse($this->output, $statusCode);
31+
}
32+
33+
/**
34+
* @param array $errors
35+
* @param string $message
36+
* @param int $statusCode
37+
* @return JsonResponse
38+
*/
39+
public function sendError($errors = [], string $message = '', int $statusCode = 404): JsonResponse
40+
{
41+
$this->output['success'] = false;
42+
$this->output['errors'] = $errors;
43+
$this->output['message'] = $message;
44+
return $this->setResponse($this->output, $statusCode);
45+
}
46+
47+
/**
48+
* @param $e
49+
* @param $statusCode
50+
* @return JsonResponse
51+
*/
52+
public function sendExceptionError($e, $statusCode = 500): JsonResponse
53+
{
54+
$message = 'internal error';
55+
if (config('app.debug')) {
56+
$message = $e->getMessage() . ' LINE:' . $e->getLine();
57+
}
58+
$this->output['success'] = false;
59+
$this->output['message'] = $message;
60+
return $this->setResponse($this->output, $statusCode);
61+
}
62+
63+
/**
64+
* @param array $data
65+
* @param int $code
66+
* @return JsonResponse
67+
*/
68+
private function setResponse(array $data = [], int $code = 200): JsonResponse
69+
{
70+
return response()->json($data, $code);
71+
}
72+
}

0 commit comments

Comments
 (0)