Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

UrlRecord::exists()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.8666
cc 4
nc 8
nop 4
crap 4
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 26
    public static function findBySlug(string $slug, string $locale): UrlRecord
23
    {
24
        // Clear the input from any trailing slashes.
25 26
        if ($slug != '/') {
26 25
            $slug = trim($slug, '/');
27
        }
28
29 26
        $record = static::where('slug', $slug)
30 26
                        ->where('locale', $locale)
31 26
                        ->orderBy('redirect_id', 'ASC')
32 26
                        ->first();
33
34 26
        if (!$record) {
35 4
            throw new UrlRecordNotFound('No url record found by slug ['.$slug.'] for locale ['.$locale.'].');
36
        }
37
38 22
        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 50
    public static function findByModel(Model $model, string $locale): UrlRecord
51
    {
52 50
        $record = static::where('model_type', $model->getMorphClass())
53 50
            ->where('model_id', $model->id)
54 50
            ->where('locale', $locale)
55 50
            ->orderBy('redirect_id', 'ASC')
56 50
            ->first();
57
58 50
        if (!$record) {
59 18
            throw new UrlRecordNotFound('No url record found for model ['.$model->getMorphClass().'@'.$model->id.'] for locale ['.$locale.'].');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
        }
61
62 34
        return $record;
63
    }
64
65 66
    public static function getByModel(Model $model)
66
    {
67 66
        return static::where('model_type', $model->getMorphClass())
68 66
                     ->where('model_id', $model->id)
69 66
                     ->get();
70
    }
71
72 14
    public function replaceAndRedirect(array $values): UrlRecord
73
    {
74 14
        $newRecord = static::create(array_merge([
75 14
            'locale'              => $this->locale,
76 14
            'model_type'          => $this->model_type,
77 14
            'model_id'            => $this->model_id,
78 14
        ], $values));
79
80 14
        $this->redirectTo($newRecord);
81
82 14
        return $newRecord;
83
    }
84
85 15
    public function redirectTo(self $record = null): ?UrlRecord
86
    {
87 15
        if (!$record) {
88 3
            return $this->isRedirect() ? static::find($this->redirect_id) : null;
89
        }
90
91 15
        $this->redirect_id = $record->id;
92 15
        $this->save();
93
94 15
        return null;
95
    }
96
97 28
    public function isRedirect(): bool
98
    {
99 28
        return !!($this->redirect_id);
100
    }
101
102 58
    public static function existsIgnoringRedirects($slug, string $locale = null, Model $ignoredModel = null): bool
103
    {
104 58
        return static::exists($slug, $locale, $ignoredModel, false);
105
    }
106
107 58
    public static function exists($slug, string $locale = null, Model $ignoredModel = null, bool $includeRedirects = true): bool
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
    {
109 58
        $builder = static::where('slug', $slug);
110
111 58
        if ($locale) {
112 58
            $builder->where('locale', $locale);
113
        }
114
115 58
        if (! $includeRedirects) {
116 58
            $builder->whereNull('redirect_id');
117
        }
118
119 58
        if ($ignoredModel) {
120
            $builder->whereNotIn('id', function ($query) use ($ignoredModel) {
121 44
                $query->select('id')
122 44
                      ->from('chief_urls')
123 44
                      ->where('model_type', '=', $ignoredModel->getMorphClass())
124 44
                      ->where('model_id', '=', $ignoredModel->id);
125 44
            });
126
        }
127
128 58
        return ($builder->count() > 0);
129
    }
130
}
131