Completed
Push — 2.0 ( 99817e...a0806d )
by Nicolas
13:38 queued 04:24
created

EloquentPageRepository::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Modules\Page\Repositories\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
7
use Modules\Page\Events\PageWasCreated;
8
use Modules\Page\Events\PageWasDeleted;
9
use Modules\Page\Events\PageWasUpdated;
10
use Modules\Page\Repositories\PageRepository;
11
12
class EloquentPageRepository extends EloquentBaseRepository implements PageRepository
13
{
14
    /**
15
     * Find the page set as homepage
16
     * @return object
17
     */
18 2
    public function findHomepage()
19
    {
20 2
        return $this->model->where('is_home', 1)->first();
21
    }
22
23
    /**
24
     * Count all records
25
     * @return int
26
     */
27
    public function countAll()
28
    {
29
        return $this->model->count();
30
    }
31
32
    /**
33
     * @param  mixed  $data
34
     * @return object
35
     */
36 3 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...
37
    {
38 3
        if (array_get($data, 'is_home') === '1') {
39 1
            $this->removeOtherHomepage();
40
        }
41 3
        $page = $this->model->create($data);
42
43 3
        event(new PageWasCreated($page->id, $data));
44
45 3
        $page->setTags(array_get($data, 'tags', []));
46
47 3
        return $page;
48
    }
49
50
    /**
51
     * @param $model
52
     * @param  array  $data
53
     * @return object
54
     */
55 1 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...
56
    {
57 1
        if (array_get($data, 'is_home') === '1') {
58
            $this->removeOtherHomepage($model->id);
59
        }
60 1
        $model->update($data);
61
62 1
        event(new PageWasUpdated($model->id, $data));
63
64 1
        $model->setTags(array_get($data, 'tags', []));
65
66 1
        return $model;
67
    }
68
69
    public function destroy($page)
70
    {
71
        $page->untag();
72
73
        event(new PageWasDeleted($page));
74
75
        return $page->delete();
76
    }
77
78
    /**
79
     * @param $slug
80
     * @param $locale
81
     * @return object
82
     */
83
    public function findBySlugInLocale($slug, $locale)
84
    {
85
        if (method_exists($this->model, 'translations')) {
86
            return $this->model->whereHas('translations', function (Builder $q) use ($slug, $locale) {
87
                $q->where('slug', $slug);
88
                $q->where('locale', $locale);
89
            })->with('translations')->first();
90
        }
91
92
        return $this->model->where('slug', $slug)->where('locale', $locale)->first();
93
    }
94
95
    /**
96
     * Set the current page set as homepage to 0
97
     * @param null $pageId
98
     */
99 1
    private function removeOtherHomepage($pageId = null)
100
    {
101 1
        $homepage = $this->findHomepage();
102 1
        if ($homepage === null) {
103 1
            return;
104
        }
105 1
        if ($pageId === $homepage->id) {
106
            return;
107
        }
108
109 1
        $homepage->is_home = 0;
110 1
        $homepage->save();
111 1
    }
112
}
113