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){ |
|
|
|
|
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){ |
|
|
|
|
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; |
|
|
|
|
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
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.