Conditions | 11 |
Paths | 5 |
Total Lines | 59 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
74 | public function handle() |
||
75 | { |
||
76 | $config = app('Illuminate\Contracts\Config\Repository'); |
||
77 | $client = app(Client::class); |
||
78 | |||
79 | if ($config->get('captain_hook.log.max_attempts', -1) !== -1 && $this->attempts() > $config->get('captain_hook.log.max_attempts')) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | $logging = $config->get('captain_hook.log.active'); |
||
84 | $transformer = $this->resolveCallable($config->get('captain_hook.transformer'), 'transform'); |
||
85 | $responseCallback = $this->resolveCallable($config->get('captain_hook.response_callback'), 'handle'); |
||
86 | |||
87 | foreach ($this->webhooks as $webhook) { |
||
88 | if ($logging) { |
||
89 | if ($config->get('captain_hook.log.storage_quantity') != -1 && |
||
90 | $webhook->logs()->count() >= $config->get('captain_hook.log.storage_quantity')) { |
||
91 | $webhook->logs()->orderBy('updated_at', 'asc')->first()->delete(); |
||
92 | } |
||
93 | $log = new WebhookLog([ |
||
94 | 'webhook_id' => $webhook['id'], |
||
95 | 'url' => $webhook['url'], |
||
96 | ]); |
||
97 | $middleware = Middleware::tap(function (RequestInterface $request, $options) use ($log) { |
||
|
|||
98 | $log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null; |
||
99 | $log->payload = $request->getBody()->getContents(); |
||
100 | }, function ($request, $options, PromiseInterface $response) use ($log, $webhook, $responseCallback) { |
||
101 | $response->then(function (ResponseInterface $response) use ($log, $webhook, $responseCallback) { |
||
102 | $log->status = $response->getStatusCode(); |
||
103 | $log->response = $response->getBody()->getContents(); |
||
104 | $log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null; |
||
105 | |||
106 | $log->save(); |
||
107 | |||
108 | // Retry this job if the webhook response didn't give us a HTTP 200 OK |
||
109 | if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) { |
||
110 | $this->release(30); |
||
111 | } |
||
112 | |||
113 | $responseCallback($webhook, $response); |
||
114 | }); |
||
115 | }); |
||
116 | |||
117 | $client->post($webhook['url'], [ |
||
118 | 'exceptions' => false, |
||
119 | 'body' => $transformer($this->eventData, $webhook), |
||
120 | 'verify' => false, |
||
121 | 'handler' => $middleware($client->getConfig('handler')), |
||
122 | ]); |
||
123 | } else { |
||
124 | $client->post($webhook['url'], [ |
||
125 | 'exceptions' => false, |
||
126 | 'body' => $transformer($this->eventData, $webhook), |
||
127 | 'verify' => false, |
||
128 | 'timeout' => 10, |
||
129 | ]); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.