1 | <?php |
||
2 | |||
3 | namespace GoCardlessPayment; |
||
4 | |||
5 | use GoCardlessPayment\Contracts\LocalCustomerRepository; |
||
6 | use GoCardlessPayment\Jobs\WebhookHandlers\WebhookEventHandlerJob; |
||
7 | use GoCardlessPayment\Models\GoCardlessMandate; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | use Illuminate\Support\Arr; |
||
10 | use Illuminate\Support\Facades\App; |
||
11 | |||
12 | class GoCardlessPayment |
||
13 | { |
||
14 | public static string $syncMetadataKeyName = 'crm_id'; |
||
15 | |||
16 | public static bool $useRoutes = true; |
||
17 | |||
18 | public static bool $runsMigrations = true; |
||
19 | |||
20 | public static array $webhookJobsMap = []; |
||
21 | |||
22 | protected static array $models = [ |
||
23 | 'mandate' => GoCardlessMandate::class, |
||
24 | ]; |
||
25 | |||
26 | public static function ignoreRoutes(): static |
||
27 | { |
||
28 | static::$useRoutes = false; |
||
29 | |||
30 | return new static; |
||
31 | } |
||
32 | |||
33 | public static function ignoreMigrations(): static |
||
34 | { |
||
35 | static::$runsMigrations = false; |
||
36 | |||
37 | return new static; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @throws \Exception |
||
42 | */ |
||
43 | public static function useModel(string $key, string $modelClass): static |
||
44 | { |
||
45 | if (! in_array($key, array_keys(static::$models))) { |
||
46 | throw new \Exception( |
||
47 | "Incorrect model key [{$key}], allowed keys are: ".implode(', ', array_keys(static::$models)) |
||
48 | ); |
||
49 | } |
||
50 | if (! is_subclass_of($modelClass, Model::class)) { |
||
51 | throw new \Exception("Class should be a model [{$modelClass}]"); |
||
52 | } |
||
53 | |||
54 | static::$models[$key] = $modelClass; |
||
55 | |||
56 | return new static(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return class-string<Model|GoCardlessMandate> |
||
61 | * |
||
62 | * @throws \Exception |
||
63 | */ |
||
64 | 4 | public static function modelClass(string $key): string |
|
65 | { |
||
66 | 4 | return static::$models[$key] ?? throw new \Exception( |
|
67 | 4 | "Incorrect model key [{$key}], allowed keys are: ".implode(', ', array_keys(static::$models)) |
|
68 | 4 | ); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return Model|GoCardlessMandate |
||
73 | * |
||
74 | * @throws \Exception |
||
75 | */ |
||
76 | public static function model(string $key, array $attributes = []): Model |
||
77 | { |
||
78 | $modelClass = static::modelClass($key); |
||
79 | |||
80 | /** @var Model $model */ |
||
81 | $model = new $modelClass($attributes); |
||
82 | |||
83 | return $model; |
||
84 | } |
||
85 | |||
86 | 6 | public static function webhookJobsMap(?array $map = null, $merge = true): array |
|
87 | { |
||
88 | 6 | $map = config('gocardless-payment.webhook_jobs'); |
|
89 | |||
90 | 6 | if (is_array($map)) { |
|
91 | 6 | static::$webhookJobsMap = $merge && static::$webhookJobsMap |
|
0 ignored issues
–
show
|
|||
92 | 6 | ? $map + static::$webhookJobsMap : $map; |
|
93 | } |
||
94 | |||
95 | 6 | return static::$webhookJobsMap; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return class-string<WebhookEventHandlerJob>|null |
||
100 | */ |
||
101 | 6 | public static function getWebhookJob(string $key): ?string |
|
102 | { |
||
103 | 6 | $class = Arr::get(static::webhookJobsMap(), $key); |
|
104 | |||
105 | 6 | if ($class && is_subclass_of($class, WebhookEventHandlerJob::class, true)) { |
|
106 | 6 | return $class; |
|
107 | } |
||
108 | |||
109 | 1 | return null; |
|
110 | } |
||
111 | |||
112 | 8 | public static function localCustomerRepository(): LocalCustomerRepository |
|
113 | { |
||
114 | 8 | return App::make(LocalCustomerRepository::class); |
|
115 | } |
||
116 | |||
117 | 8 | public static function api(): Api |
|
118 | { |
||
119 | 8 | return App::make(Api::class); |
|
120 | } |
||
121 | } |
||
122 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.