GoCardlessPayment::model()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Model|GoCardlessMandate> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Model|GoCardlessMandate>.
Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $map is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public static function webhookJobsMap(/** @scrutinizer ignore-unused */ ?array $map = null, $merge = true): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
Bug Best Practice introduced by
The expression static::webhookJobsMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
92 6
                ? $map + static::$webhookJobsMap : $map;
93
        }
94
95 6
        return static::$webhookJobsMap;
96
    }
97
98
    /**
99
     * @return class-string<WebhookEventHandlerJob>|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<WebhookEventHandlerJob>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<WebhookEventHandlerJob>|null.
Loading history...
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