HomePageComposer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 28
loc 119
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B compose() 28 78 6

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
2
3
namespace App\Http\ViewComposers;
4
5
use App\Product;
6
use App\Repositories\CategoryRepository;
7
use App\Repositories\PostsRepository;
8
use App\Repositories\ProductsRepository;
9
use Illuminate\Contracts\View\View;
10
11
class HomePageComposer extends Composer
12
{
13
    /**
14
     * @var ProductsRepository
15
     */
16
    protected $products;
17
18
    /**
19
     * @var CategoryRepository
20
     */
21
    protected $categories;
22
23
    /**
24
     * @var PostsRepository
25
     */
26
    protected $posts;
27
28
    /**
29
     * HomePageComposer constructor.
30
     * @param ProductsRepository $productsRepository
31
     * @param CategoryRepository $categoryRepository
32
     * @param PostsRepository $postsRepository
33
     */
34
    public function __construct(
35
        ProductsRepository $productsRepository,
36
        CategoryRepository $categoryRepository,
37
        PostsRepository $postsRepository
38
    )
39
    {
40
        $this->products = $productsRepository;
41
        $this->categories = $categoryRepository;
42
        $this->posts = $postsRepository;
43
    }
44
45
    /**
46
     * Bind data to view.
47
     *
48
     * @param View $view
49
     * @return View
50
     */
51
    public function compose(View $view)
52
    {
53
        switch ($view->getName()) {
54
            case "home.index":
55
                $category_1 = $this->categories->find(settings()->getOption('home::category_first'));
56
                $category_2 = $this->categories->find(settings()->getOption('home::category_second'));
57
58
                return $view
59
                    ->with('popular_category', $this->categories->getPopularCategory())
60
                    ->with('recommended', [
61
                        'name' => 'oferte recomandate',
62
                        'data' => function () {
63
                            return $this->products->getFeaturedPublic(5);
64
                        }
65
                    ])
66
                    ->with('posts', [
67
                        'name' => 'articole de blog',
68
                        'data' => function (){
69
                            return $this->posts->getPopularPublic();
70
                        }
71
                    ])
72
                    ->with('expire', [
73
                        'name' => 'produse care expira in curand',
74
                        'label' => 'info_corner.png',
75
                        'data' => function ($count = 8) {
76
                            return $this->products->getPublicExpireSoon($count);
77
                        }
78
                    ])
79
                    ->with('popular', [
80
                        'name' => 'produse populare',
81
                        'data' => function () {
82
                            return $this->products->getFeaturedPublic(8);
83
                        }
84
                    ])
85
                    ->with('category_1', [
86
                        'name' => ($category_1) ? $category_1->name : '',
87 View Code Duplication
                        'data' => function () use ($category_1){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
                            $products = [];
89
90
                            if($category_1)
91
                            $category_1->categoryables()
92
                                ->active()
93
                                ->elementType(get_class($this->products->getModel()))
94
                                ->get()
95
                                ->each(function ($morph) use (&$products) {
96
                                    $products[] = $morph->categoryable;
97
                                });
98
99
                            return $products;
100
                        }
101
                    ])
102
                    ->with('category_2', [
103
                        'name' => ($category_2) ? $category_2->name : '',
104 View Code Duplication
                        'data' => function () use ($category_2){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
                            $products = [];
106
107
                            if($category_2)
108
                            $category_2->categoryables()
109
                                ->active()
110
                                ->elementType(get_class($this->products->getModel()))
111
                                ->get()
112
                                ->each(function ($morph) use (&$products) {
113
                                    $products[] = $morph->categoryable;
114
                                });
115
116
                            return $products;
117
                        }
118
                    ])
119
                    ->with('latest', [
120
                        'name' => 'latest',
121
                        'data' => function () {
122
                            return $this->products->getPublicLatest();
123
                        }
124
                    ]);
125
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
126
127
        }
128
    }
129
}