Passed
Push — main ( 2a66c5...5262fa )
by Yaroslav
03:05
created

RestrictedLink::getAccessConfig()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.9666
1
<?php
2
3
namespace LinkRestrictedAccess\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Casts\Attribute;
8
use Illuminate\Database\Eloquent\Factories\HasFactory;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
use Illuminate\Support\Str;
13
use JsonFieldCast\Casts\SimpleJsonField;
14
use LinkRestrictedAccess\Database\Factories\RestrictedLinkFactory;
15
use LinkRestrictedAccess\RestrictedAccess;
16
17
/**
18
 * @property \JsonFieldCast\Json\SimpleJsonField $access_configuration
19
 * @property \JsonFieldCast\Json\SimpleJsonField $meta
20
 */
21
class RestrictedLink extends Model
22
{
23
    use HasFactory;
24
25
    protected $guarded = [];
26
27
    protected $casts = [
28
        'use_pin'              => 'datetime',
29
        'access_configuration' => SimpleJsonField::class,
30
        'meta'                 => SimpleJsonField::class,
31
    ];
32
33
    protected $accessConfigsMap = [
34
        'checkName'               => 'use_name',
35
        'checkEmail'              => 'use_email',
36
    ];
37
38 14
    public function getTable(): string
39
    {
40 14
        return config('restricted-access.tables.links');
41
    }
42
43 15
    protected static function boot(): void
44
    {
45 15
        parent::boot();
46
47 15
        static::saving(function ($model) {
48 14
            if (!$model->uuid) {
49 14
                $model->uuid = (string)Str::uuid();
50
            }
51 15
        });
52
    }
53
54 3
    public function linkable(): MorphTo
55
    {
56 3
        return $this->morphTo('linkable');
57
    }
58
59
60 7
    public function openActions(): HasMany
61
    {
62 7
        return $this->hasMany(RestrictedAccess::linkOpenActionModel(), 'link_id', 'id');
63
    }
64
65 6
    public function checkPin(): Attribute
66
    {
67 6
        return Attribute::make(
68 6
            fn () => (bool)$this->use_pin,
0 ignored issues
show
Bug introduced by
The property use_pin does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
69 6
            fn ($value) => [
70 6
                'use_pin' => $value ? $this->use_pin ?? Carbon::now() : null,
0 ignored issues
show
Bug introduced by
The property use_pin does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
71 6
            ],
72 6
        );
73
    }
74
75 5
    protected function getAccessConfig(string $key): Attribute
76
    {
77 5
        $field = $this->accessConfigsMap[$key]??Str::snake($key);
78
79 5
        return Attribute::make(
80 5
            fn () => (bool)$this->access_configuration->getDateAttribute($field),
81 5
            function ($value) use ($field) {
82 2
                if ($value) {
83 2
                    $this->access_configuration->setDate($field, $this->access_configuration->getDateAttribute($field, Carbon::now()));
84
                } else {
85 1
                    $this->access_configuration->removeAttribute($field);
86
                }
87
88 2
                return [];
89 5
            },
90 5
        );
91
    }
92
93 5
    public function checkName(): Attribute
94
    {
95 5
        return $this->getAccessConfig(__FUNCTION__);
96
    }
97
98 4
    public function checkEmail(): Attribute
99
    {
100 4
        return $this->getAccessConfig(__FUNCTION__);
101
    }
102
103 1
    public function needVerification(): bool
104
    {
105 1
        return $this->check_pin ||
0 ignored issues
show
Bug introduced by
The property check_pin does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
106 1
            $this->check_email  ||
0 ignored issues
show
Bug introduced by
The property check_email does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
107 1
            $this->check_name;
0 ignored issues
show
Bug introduced by
The property check_name does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
108
    }
109
110 1
    public function link(): string
111
    {
112 1
        return $this->linkable->restrictedLink($this);
0 ignored issues
show
Bug introduced by
The property linkable does not seem to exist on LinkRestrictedAccess\Models\RestrictedLink. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
113
    }
114
115 4
    public function cookieName(): string
116
    {
117 4
        return "ral_{$this->uuid}";
118
    }
119
120 1
    public function openActionFromCookie(\Illuminate\Http\Request $request): ?RestrictedLinkOpenAction
121
    {
122 1
        $openUuid = $request->cookie($this->cookieName());
123 1
        if ($openUuid) {
124 1
            return RestrictedAccess::linkOpenActionModel()::query()->where('uuid', $openUuid)->first();
125
        }
126
127
        return null;
128
    }
129
130 1
    public function verifiedOpenActionFromCookie(\Illuminate\Http\Request $request): ?RestrictedLinkOpenAction
131
    {
132 1
        $open = $this->openActionFromCookie($request);
133 1
        if($open?->verified($request)) {
134 1
            return $open;
135
        }
136
137 1
        return null;
138
    }
139
140 1
    public static function scopeByKey(Builder $query, string $uuid, Model|int|null $relatedModel = null): Builder
141
    {
142 1
        $query->where('uuid', $uuid);
143
144 1
        if ($relatedModel) {
145 1
            if ($relatedModel instanceof Model) {
146 1
                $relatedModel = $relatedModel->getKey();
147
            }
148
149 1
            $query->whereHas('linkable', fn (Builder $q) => $q->whereKey($relatedModel));
150
        }
151
152 1
        return $query;
153
    }
154
155 14
    protected static function newFactory(): RestrictedLinkFactory
156
    {
157 14
        return RestrictedLinkFactory::new();
158
    }
159
}
160