Conditions | 8 |
Paths | 162 |
Total Lines | 53 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
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 |
||
57 | public function handle() |
||
58 | { |
||
59 | /** @var \GuzzleHttp\Client $client */ |
||
60 | $client = app(Client::class); |
||
61 | |||
62 | $lastAttempt = $this->attempts() >= $this->tries; |
||
63 | |||
64 | try { |
||
65 | $body = strtoupper($this->httpVerb) === 'GET' |
||
66 | ? ['query' => $this->payload] |
||
67 | : ['body' => json_encode($this->payload)]; |
||
68 | |||
69 | $this->response = $client->request($this->httpVerb, $this->webhookUrl, array_merge([ |
||
70 | 'timeout' => $this->requestTimeout, |
||
71 | 'verify' => $this->verifySsl, |
||
72 | 'headers' => $this->headers, |
||
73 | ], $body)); |
||
74 | |||
75 | if (! Str::startsWith($this->response->getStatusCode(), 2)) { |
||
76 | throw new Exception('Webhook call failed'); |
||
77 | } |
||
78 | |||
79 | $this->dispatchEvent(WebhookCallSucceededEvent::class); |
||
80 | |||
81 | return; |
||
82 | } catch (Exception $exception) { |
||
83 | if ($exception instanceof RequestException) { |
||
84 | $this->response = $exception->getResponse(); |
||
85 | $this->errorType = get_class($exception); |
||
86 | $this->errorMessage = $exception->getMessage(); |
||
87 | } |
||
88 | |||
89 | if ($exception instanceof ConnectException) { |
||
90 | $this->errorType = get_class($exception); |
||
91 | $this->errorMessage = $exception->getMessage(); |
||
92 | } |
||
93 | |||
94 | if (! $lastAttempt) { |
||
95 | /** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */ |
||
96 | $backoffStrategy = app($this->backoffStrategyClass); |
||
97 | |||
98 | $waitInSeconds = $backoffStrategy->waitInSecondsAfterAttempt($this->attempts()); |
||
99 | |||
100 | $this->release($waitInSeconds); |
||
101 | } |
||
102 | |||
103 | $this->dispatchEvent(WebhookCallFailedEvent::class); |
||
104 | } |
||
105 | |||
106 | if ($lastAttempt) { |
||
107 | $this->dispatchEvent(FinalWebhookCallFailedEvent::class); |
||
108 | |||
109 | $this->delete(); |
||
110 | } |
||
140 |