1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finder\Http\Actions; |
4
|
|
|
|
5
|
|
|
use Finder\Http\Requests\ReCaptchaRequest; |
6
|
|
|
use Finder\Http\Resources\SubscriptionPlainResource; |
7
|
|
|
use Population\Manipule\Managers\SubscriptionManager; |
8
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
9
|
|
|
use Illuminate\Http\JsonResponse; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SubscriptionCreateAction. |
13
|
|
|
* |
14
|
|
|
* @package Finder\Http\Actions |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class SubscriptionCreateAction |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ResponseFactory |
20
|
|
|
*/ |
21
|
|
|
private $responseFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SubscriptionManager |
25
|
|
|
*/ |
26
|
|
|
private $subscriptionManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SubscriptionCreateAction constructor. |
30
|
|
|
* |
31
|
|
|
* @param ResponseFactory $responseFactory |
32
|
|
|
* @param SubscriptionManager $subscriptionManager |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ResponseFactory $responseFactory, SubscriptionManager $subscriptionManager) |
35
|
|
|
{ |
36
|
|
|
$this->responseFactory = $responseFactory; |
37
|
|
|
$this->subscriptionManager = $subscriptionManager; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @apiVersion 1.0.0 |
42
|
|
|
* @api {post} /v1/subscriptions Create |
43
|
|
|
* @apiName Create |
44
|
|
|
* @apiGroup Subscriptions |
45
|
|
|
* @apiHeader {String} Accept application/json |
46
|
|
|
* @apiHeader {String} Content-Type application/json |
47
|
|
|
* @apiParamExample {json} Request-Body-Example: |
48
|
|
|
* { |
49
|
|
|
* "email": "[email protected]" |
50
|
|
|
* } |
51
|
|
|
* @apiSuccessExample {json} Success-Response: |
52
|
|
|
* HTTP/1.1 201 Created |
53
|
|
|
* { |
54
|
|
|
* "email": "[email protected]", |
55
|
|
|
* "token": "subscription_token_string" |
56
|
|
|
* } |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a subscription. |
61
|
|
|
* |
62
|
|
|
* @param ReCaptchaRequest $request |
63
|
|
|
* @return JsonResponse |
64
|
|
|
*/ |
65
|
|
|
public function __invoke(ReCaptchaRequest $request): JsonResponse |
66
|
|
|
{ |
67
|
|
|
$subscription = $this->subscriptionManager->create($request->all()); |
68
|
|
|
|
69
|
|
|
return $this->responseFactory->json(new SubscriptionPlainResource($subscription), JsonResponse::HTTP_CREATED); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.