Nestednet /
gocardless-laravel
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: eudaldarranztresserra |
||
| 5 | * Date: 2019-03-01 |
||
| 6 | * Time: 13:08 |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Nestednet\Gocardless; |
||
| 10 | |||
| 11 | use Exception; |
||
| 12 | use Illuminate\Database\Eloquent\Model; |
||
| 13 | use Nestednet\Gocardless\Exceptions\WebhookFailed; |
||
| 14 | |||
| 15 | class GocardlessWebhookCall extends Model |
||
| 16 | { |
||
| 17 | public $guarded = []; |
||
| 18 | |||
| 19 | protected $casts = [ |
||
| 20 | 'payload' => 'array', |
||
| 21 | 'exception' => 'array', |
||
| 22 | ]; |
||
| 23 | |||
| 24 | public function process() |
||
| 25 | { |
||
| 26 | $this->clearException(); |
||
| 27 | |||
| 28 | if ($this->resource_type === '') { |
||
| 29 | throw WebhookFailed::missingResource($this); |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($this->action === '') { |
||
| 33 | throw WebhookFailed::missingAction($this); |
||
| 34 | } |
||
| 35 | |||
| 36 | event("gocardless-webhooks::{$this->resource_type}_{$this->action}", $this); |
||
| 37 | |||
| 38 | $jobClass = $this->determineJobClass($this->resource_type, $this->action); |
||
| 39 | |||
| 40 | if ($jobClass === "") { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (! class_exists($jobClass)) { |
||
| 45 | throw WebhookFailed::jobClassDoesNotExist($jobClass, $this); |
||
| 46 | } |
||
| 47 | |||
| 48 | dispatch(new $jobClass($this)); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function saveException(Exception $exception) |
||
| 52 | { |
||
| 53 | $this->exception = [ |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 54 | 'code' => $exception->getCode(), |
||
| 55 | 'message' => $exception->getMessage(), |
||
| 56 | 'trace' => $exception->getTraceAsString(), |
||
| 57 | ]; |
||
| 58 | |||
| 59 | $this->save(); |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function determineJobClass(string $resourceType, string $action) : string |
||
| 65 | { |
||
| 66 | $formattedResourceType = str_replace('.', '_', $resourceType); |
||
| 67 | $formattedAction = str_replace('.', '_', $action); |
||
| 68 | |||
| 69 | $jobClassKey = "{$formattedResourceType}_{$formattedAction}"; |
||
| 70 | |||
| 71 | return config("gocardless.webhooks.jobs.{$jobClassKey}", ""); |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function clearException() |
||
| 75 | { |
||
| 76 | $this->exception = null; |
||
|
0 ignored issues
–
show
|
|||
| 77 | |||
| 78 | $this->save(); |
||
| 79 | |||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | } |