Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

UrlRecord::isHomepage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class UrlRecord extends Model
8
{
9
    public $table = 'chief_urls';
10
11
    public $guarded = [];
12
13
    /**
14
     * Find matching url record for passed slug and locale. The locale parameter will try
15
     * to match specific given locales first and records without locale as fallback.
16
     *
17
     * @param string $slug
18
     * @param string $locale
19
     * @return UrlRecord
20
     * @throws UrlRecordNotFound
21
     */
22 29
    public static function findBySlug(string $slug, string $locale): UrlRecord
23
    {
24
        // Clear the input from any trailing slashes.
25 29
        if ($slug != '/') {
26 27
            $slug = trim($slug, '/');
27
        }
28
29 29
        $record = static::where('slug', $slug)
30 29
                        ->where('locale', $locale)
31 29
                        ->orderBy('redirect_id', 'ASC')
32 29
                        ->first();
33
34 29
        if (!$record) {
35 4
            throw new UrlRecordNotFound('No url record found by slug ['.$slug.'] for locale ['.$locale.'].');
36
        }
37
38 25
        return $record;
39
    }
40
41
    /**
42
     * Find matching url record for passed slug and locale. The locale parameter will try
43
     * to match specific given locales first and records without locale as fallback.
44
     *
45
     * @param Model $model
46
     * @param string $locale
47
     * @return UrlRecord
48
     * @throws UrlRecordNotFound
49
     */
50 59
    public static function findByModel(Model $model, string $locale): UrlRecord
51
    {
52 59
        $record = static::where('model_type', $model->getMorphClass())
53 59
            ->where('model_id', $model->id)
54 59
            ->where('locale', $locale)
55 59
            ->orderBy('redirect_id', 'ASC')
56 59
            ->first();
57
58 59
        if (!$record) {
59 21
            throw new UrlRecordNotFound('No url record found for model ['.$model->getMorphClass().'@'.$model->id.'] for locale ['.$locale.'].');
60
        }
61
62 40
        return $record;
63
    }
64
65 76
    public static function getByModel(Model $model)
66
    {
67 76
        return static::where('model_type', $model->getMorphClass())
68 76
                     ->where('model_id', $model->id)
69 76
                     ->get();
70
    }
71
72 1
    public static function findRecentRedirect(Model $model, string $locale): ?self
73
    {
74 1
        return static::where('model_type', $model->getMorphClass())
75 1
            ->where('model_id', $model->id)
76 1
            ->where('locale', $locale)
77 1
            ->where('redirect_id', '<>', null)
78 1
            ->orderBy('updated_at', 'DESC')
79 1
            ->first();
80
    }
81
82 17
    public function replaceAndRedirect(array $values): UrlRecord
83
    {
84 17
        $newRecord = static::create(array_merge([
85 17
            'locale'              => $this->locale,
86 17
            'model_type'          => $this->model_type,
87 17
            'model_id'            => $this->model_id,
88 17
        ], $values));
89
90 17
        $this->redirectTo($newRecord);
91
92 17
        return $newRecord;
93
    }
94
95 18
    public function redirectTo(self $record = null): ?UrlRecord
96
    {
97 18
        if (!$record) {
98 3
            return $this->isRedirect() ? static::find($this->redirect_id) : null;
99
        }
100
101 18
        $this->redirect_id = $record->id;
102 18
        $this->save();
103
104 18
        return null;
105
    }
106
107
    // Remove all urls that came after this one
108 1
    public function revert()
109
    {
110 1
        if (!$this->isRedirect()) {
111 1
            return;
112
        }
113
114
        // Remove this redirect relation so it's no longer cascading when main url is getting deleted.
115 1
        $redirect_id = $this->redirect_id;
116 1
        $this->redirect_id = null;
117 1
        $this->save();
118
119 1
        if ($record = static::where('id', $redirect_id)->first()) {
120 1
            $record->revert();
121 1
            $record->delete();
122
        }
123 1
    }
124
125 75
    public function isRedirect(): bool
126
    {
127 75
        return !!($this->redirect_id);
128
    }
129
130 66
    public function isHomepage(): bool
131
    {
132 66
        return $this->slug === '/';
133
    }
134
135 66
    public static function existsIgnoringRedirects($slug, string $locale = null, Model $ignoredModel = null): bool
136
    {
137 66
        return static::exists($slug, $locale, $ignoredModel, false);
138
    }
139
140 66
    public static function exists($slug, string $locale = null, Model $ignoredModel = null, bool $includeRedirects = true): bool
141
    {
142 66
        $builder = static::where('slug', $slug);
143
144 66
        if ($locale) {
145 66
            $builder->where('locale', $locale);
146
        }
147
148 66
        if (! $includeRedirects) {
149 66
            $builder->whereNull('redirect_id');
150
        }
151
152 66
        if ($ignoredModel) {
153
            $builder->whereNotIn('id', function ($query) use ($ignoredModel) {
154 48
                $query->select('id')
155 48
                      ->from('chief_urls')
156 48
                      ->where('model_type', '=', $ignoredModel->getMorphClass())
157 48
                      ->where('model_id', '=', $ignoredModel->id);
158 48
            });
159
        }
160
161 66
        return ($builder->count() > 0);
162
    }
163
}
164