Completed
Push — master ( 6eb122...562c4b )
by Nicolas
14:47
created

EloquentPageRepository::removeOtherHomepage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
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\PageWasUpdated;
7
use Modules\Page\Repositories\PageRepository;
8
9
class EloquentPageRepository extends EloquentBaseRepository implements PageRepository
10
{
11
    /**
12
     * Find the page set as homepage
13
     * @return object
14
     */
15
    public function findHomepage()
16
    {
17
        return $this->model->where('is_home', 1)->first();
18
    }
19
20
    /**
21
     * Count all records
22
     * @return int
23
     */
24
    public function countAll()
25
    {
26
        return $this->model->count();
27
    }
28
29
    /**
30
     * @param  mixed  $data
31
     * @return object
32
     */
33 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...
34
    {
35
        if (array_get($data, 'is_home') === '1') {
36
            $this->removeOtherHomepage();
37
        }
38
        $page = $this->model->create($data);
39
40
        event(new PageWasCreated($page->id, $data));
41
42
        return $page;
43
    }
44
45
    /**
46
     * @param $model
47
     * @param  array  $data
48
     * @return object
49
     */
50 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...
51
    {
52
        if (array_get($data, 'is_home') === '1') {
53
            $this->removeOtherHomepage();
54
        }
55
        $model->update($data);
56
57
        event(new PageWasUpdated($model->id, $data));
58
59
        return $model;
60
    }
61
62
    /**
63
     * @param $slug
64
     * @param $locale
65
     * @return object
66
     */
67
    public function findBySlugInLocale($slug, $locale)
68
    {
69
        if (method_exists($this->model, 'translations')) {
70
            return $this->model->whereHas('translations', function (Builder $q) use ($slug, $locale) {
71
                $q->where('slug', $slug);
72
                $q->where('locale', $locale);
73
            })->with('translations')->first();
74
        }
75
76
        return $this->model->where('slug', $slug)->where('locale', $locale)->first();
77
    }
78
79
    /**
80
     * Set the current page set as homepage to 0
81
     */
82
    private function removeOtherHomepage()
83
    {
84
        $homepage = $this->findHomepage();
85
        if ($homepage === null) {
86
            return;
87
        }
88
        $homepage->is_home = 0;
89
        $homepage->save();
90
    }
91
}
92