Passed
Push — master ( 0470af...c48ca3 )
by Quentin
05:29
created

HandleSlugs::afterRestoreHandleSlugs()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
trait HandleSlugs
6
{
7 17
    public function afterSaveHandleSlugs($object, $fields)
8
    {
9 17
        if (property_exists($this->model, 'slugAttributes')) {
10 17
            foreach (getLocales() as $locale) {
11 17
                if (isset($fields['slug']) && isset($fields['slug'][$locale]) && !empty($fields['slug'][$locale])) {
12 17
                    $object->disableLocaleSlugs($locale);
13 17
                    $currentSlug = [];
14 17
                    $currentSlug['slug'] = $fields['slug'][$locale];
15 17
                    $currentSlug['locale'] = $locale;
16 17
                    $currentSlug['active'] = property_exists($this->model, 'translatedAttributes') ? $object->translate($locale)->active : 1;
17 17
                    $currentSlug = $this->getSlugParameters($object, $fields, $currentSlug);
18 17
                    $object->updateOrNewSlug($currentSlug);
19
                }
20
            }
21
        }
22 17
    }
23
24 2
    public function afterDeleteHandleSlugs($object)
25
    {
26 2
        $object->slugs()->delete();
27 2
    }
28
29 1
    public function afterRestoreHandleSlugs($object)
30
    {
31 1
        $object->slugs()->restore();
32 1
    }
33
34 4
    public function getFormFieldsHandleSlugs($object, $fields)
35
    {
36 4
        unset($fields['slugs']);
37
38 4
        if ($object->slugs != null) {
39 4
            foreach ($object->slugs as $slug) {
40 4
                if ($slug->active || $object->slugs->where('locale', $slug->locale)->where('active', true)->count() === 0) {
41 4
                    $fields['translations']['slug'][$slug->locale] = $slug->slug;
42
                }
43
            }
44
        }
45
46 4
        return $fields;
47
    }
48
49 17
    public function getSlugParameters($object, $fields, $slug)
50
    {
51 17
        $slugParams = $object->getSlugParams($slug['locale']);
52
53 17
        foreach ($object->slugAttributes as $param) {
54 17
            if (isset($slugParams[$param]) && isset($fields[$param])) {
55
                $slug[$param] = $fields[$param];
56 17
            } elseif (isset($slugParams[$param])) {
57
                $slug[$param] = $slugParams[$param];
58
            }
59
        }
60
61 17
        return $slug;
62
    }
63
64
    public function forSlug($slug, $with = [], $withCount = [], $scopes = [])
65
    {
66
        $query = $this->model->where($scopes)->scopes(['published', 'visible']);
67
68
        foreach (class_uses_recursive(get_called_class()) as $trait) {
69
            if (method_exists(get_called_class(), $method = 'getPublishedScopes' . class_basename($trait))) {
70
                $query->scopes($this->$method());
71
            }
72
        }
73
74
        $item = (clone $query)->forSlug($slug)->with($with)->withCount($withCount)->first();
75
76
        if (!$item && $item = (clone $query)->forInactiveSlug($slug)->first()) {
77
            $item->redirect = true;
78
        }
79
80
        if (!$item && config('translatable.use_property_fallback', false)
81
        && config('translatable.fallback_locale') != config('app.locale')) {
82
            $item = (clone $query)->orWhere(function ($query) {
83
                return $query->withActiveTranslations(config('translatable.fallback_locale'));
84
            })->forFallbackLocaleSlug($slug)->first();
85
86
            if ($item) {
87
                $item->redirect = true;
88
            }
89
        }
90
91
        return $item;
92
    }
93
94
    public function forSlugPreview($slug, $with = [], $withCount = [])
95
    {
96
        return $this->model->forInactiveSlug($slug)->with($with)->withCount($withCount)->first();
97
    }
98
}
99