Completed
Push — master ( 700fc0...fb0c07 )
by Nicolas
02:12
created

EloquentPageRepository::findBySlugInLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php namespace Modules\Page\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
5
use Modules\Page\Events\PageWasCreated;
6
use Modules\Page\Events\PageWasDeleted;
7
use Modules\Page\Events\PageWasUpdated;
8
use Modules\Page\Repositories\PageRepository;
9
10
class EloquentPageRepository extends EloquentBaseRepository implements PageRepository
11
{
12
    /**
13
     * Find the page set as homepage
14
     * @return object
15
     */
16
    public function findHomepage()
17
    {
18
        return $this->model->where('is_home', 1)->first();
19
    }
20
21
    /**
22
     * Count all records
23
     * @return int
24
     */
25
    public function countAll()
26
    {
27
        return $this->model->count();
28
    }
29
30
    /**
31
     * @param  mixed  $data
32
     * @return object
33
     */
34 View Code Duplication
    public function create($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        if (array_get($data, 'is_home') === '1') {
37
            $this->removeOtherHomepage();
38
        }
39
        $page = $this->model->create($data);
40
41
        event(new PageWasCreated($page->id, $data));
42
43
        return $page;
44
    }
45
46
    /**
47
     * @param $model
48
     * @param  array  $data
49
     * @return object
50
     */
51 View Code Duplication
    public function update($model, $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (array_get($data, 'is_home') === '1') {
54
            $this->removeOtherHomepage();
55
        }
56
        $model->update($data);
57
58
        event(new PageWasUpdated($model->id, $data));
59
60
        return $model;
61
    }
62
63
    public function destroy($model)
64
    {
65
        event(new PageWasDeleted($model));
66
67
        return $model->delete();
68
    }
69
70
    /**
71
     * @param $slug
72
     * @param $locale
73
     * @return object
74
     */
75
    public function findBySlugInLocale($slug, $locale)
76
    {
77
        if (method_exists($this->model, 'translations')) {
78
            return $this->model->whereHas('translations', function (Builder $q) use ($slug, $locale) {
79
                $q->where('slug', $slug);
80
                $q->where('locale', $locale);
81
            })->with('translations')->first();
82
        }
83
84
        return $this->model->where('slug', $slug)->where('locale', $locale)->first();
85
    }
86
87
    /**
88
     * Set the current page set as homepage to 0
89
     */
90
    private function removeOtherHomepage()
91
    {
92
        $homepage = $this->findHomepage();
93
        if ($homepage === null) {
94
            return;
95
        }
96
        $homepage->is_home = 0;
97
        $homepage->save();
98
    }
99
}
100