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

EloquentPageRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 26.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 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 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\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