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

EloquentPageRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 24.44 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 11
c 6
b 0
f 1
lcom 1
cbo 6
dl 22
loc 90
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findHomepage() 0 4 1
A countAll() 0 4 1
A create() 11 11 2
A update() 11 11 2
A destroy() 0 6 1
A findBySlugInLocale() 0 11 2
A removeOtherHomepage() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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