Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
CRAP | |
95.71% |
67 / 70 |
SiteTrackerConfigController | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
19 | |
95.71% |
67 / 70 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
index | |
100.00% |
1 / 1 |
1 | |
100.00% |
15 / 15 |
|||
store | |
0.00% |
0 / 1 |
5.01 | |
92.86% |
13 / 14 |
|||
show | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
|||
update | |
0.00% |
0 / 1 |
6.04 | |
90.00% |
18 / 20 |
|||
destroy | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
validateData | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Site\Controllers; |
4 | |
5 | use App\Http\Controllers\Controller; |
6 | use Illuminate\Http\Response; |
7 | use Illuminate\Http\Request; |
8 | use Illuminate\Support\Arr; |
9 | use Illuminate\Support\Facades\Log; |
10 | use Illuminate\Support\Facades\Redis; |
11 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
12 | use Illuminate\Validation\Rule; |
13 | |
14 | use Qmp\Laravel\Site\Models\Site; |
15 | |
16 | class SiteTrackerConfigController extends AbstractMicroServiceController |
17 | { |
18 | use BrandTrait; |
19 | use CompanyTrait; |
20 | |
21 | public function __construct(Request $request) |
22 | { |
23 | //$this->middleware('perm:xxx'); |
24 | |
25 | parent::__construct($request); |
26 | } |
27 | |
28 | /** |
29 | * Display a listing of the resource. |
30 | * |
31 | * @return \Illuminate\Http\Response |
32 | */ |
33 | public function index() |
34 | { |
35 | $sites = Site::select(['_id', 'hash', 'siteUrl', 'siteName', 'brand', 'company', 'tracker']) |
36 | ->where('tracker', 'exists', true) |
37 | ->get() |
38 | ->map(function($site) { |
39 | $config = $site->tracker; |
40 | $config['_id'] = $site->_id; |
41 | $config['hash'] = $site->hash; |
42 | $config['siteUrl'] = $site->siteUrl; |
43 | $config['siteName'] = $site->siteName; |
44 | $config['brand'] = $site->brand; |
45 | $config['company'] = $site->company; |
46 | |
47 | return $config; |
48 | |
49 | }); |
50 | |
51 | Log::debug('List of sites' . var_export($sites, true)); |
52 | |
53 | |
54 | return $sites; |
55 | } |
56 | |
57 | /** |
58 | * Store a newly created resource in storage. |
59 | * |
60 | * @param \Illuminate\Http\Request $request |
61 | * @return \Illuminate\Http\Response |
62 | */ |
63 | public function store(Request $request) |
64 | { |
65 | $validatedData = $this->validateData($request); |
66 | |
67 | $data = [ |
68 | 'hash' => $validatedData['hash'], |
69 | 'siteUrl' => $validatedData['siteUrl'], |
70 | 'siteName' => $validatedData['siteName'], |
71 | 'brand' => isset($validatedData['brand']) ? $validatedData['brand'] : null, |
72 | 'company' => isset($validatedData['company']) ? $validatedData['company'] : null, |
73 | 'tracker' => Arr::except($validatedData, ['_id', 'hash', 'siteUrl', 'siteName', 'brand', 'company']) |
74 | ]; |
75 | |
76 | $site = Site::create($data); |
77 | |
78 | if ($site->brand) { |
79 | $this->addSiteToBrand($site->brand, $site->_id); |
80 | } |
81 | |
82 | if ($site->company) { |
83 | $this->addSiteToCompany($site->company, $site->_id); |
84 | } |
85 | |
86 | Redis::publish('tracker', json_encode(['action' => 'deploy-data', 'type' => 'tag-config', 'site' => $site])); |
87 | |
88 | return $site; |
89 | } |
90 | |
91 | /** |
92 | * Display the specified resource. |
93 | * |
94 | * @return \Illuminate\Http\Response |
95 | */ |
96 | public function show($configId) |
97 | { |
98 | $site = Site::findOrFail($configId); |
99 | |
100 | $config = $site->tracker ? $site->tracker : []; |
101 | $config['_id'] = $site->_id; |
102 | $config['hash'] = $site->hash; |
103 | $config['siteUrl'] = $site->siteUrl; |
104 | $config['siteName'] = $site->siteName; |
105 | $config['brand'] = $site->brand; |
106 | $config['company'] = $site->company; |
107 | |
108 | return $config; |
109 | } |
110 | |
111 | /** |
112 | * Update the specified resource in storage. |
113 | * |
114 | * @param \Illuminate\Http\Request $request |
115 | * @return \Illuminate\Http\Response |
116 | */ |
117 | public function update(Request $request, $id) |
118 | { |
119 | $validatedData = $this->validateData($request, $id); |
120 | $model = Site::findOrFail($id); |
121 | |
122 | $oldBrand = $model->brand; |
123 | $oldCompany = $model->company; |
124 | |
125 | $model->hash = $validatedData['hash']; |
126 | $model->siteUrl = $validatedData['siteUrl']; |
127 | $model->siteName = $validatedData['siteName']; |
128 | $model->brand = isset($validatedData['brand']) ? $validatedData['brand'] : null; |
129 | $model->company = isset($validatedData['company']) ? $validatedData['company'] : null; |
130 | $model->tracker = Arr::except($validatedData, ['_id', 'hash', 'siteUrl', 'siteName', 'brand', 'company']); |
131 | $result = $model->save(); |
132 | |
133 | $model->refresh(); |
134 | |
135 | if ($model->brand) { |
136 | $this->addSiteToBrand($model->brand, $model->_id); |
137 | } else { |
138 | $this->removeSiteToBrand($oldBrand, $model->_id); |
139 | } |
140 | |
141 | if ($model->company) { |
142 | $this->addSiteToCompany($model->company, $model->_id); |
143 | } else { |
144 | $this->removeSiteToCompany($oldCompany, $model->_id); |
145 | } |
146 | |
147 | Redis::publish('tracker', json_encode(['action' => 'deploy-data', 'type' => 'tag-config', 'site' => $model])); |
148 | |
149 | return response()->json(['status' => $result ? 'ok' : 'ko']); |
150 | } |
151 | |
152 | /** |
153 | * Remove the specified resource from storage. |
154 | * |
155 | * @return \Illuminate\Http\Response |
156 | */ |
157 | public function destroy($id) |
158 | { |
159 | $model = Site::findOrFail($id); |
160 | $model->tracker = []; |
161 | $result = $model->save(); |
162 | |
163 | return response()->json(['status' => $result ? 'ok' : 'ko']); |
164 | } |
165 | |
166 | protected function validateData(Request $request, $id = null) |
167 | { |
168 | $rules =[ |
169 | 'hash' => 'required|string', |
170 | 'active' => 'boolean', |
171 | 'siteUrl' => 'required|string', |
172 | 'siteName' => 'required|string', |
173 | 'brand' => 'alpha_num|nullable', |
174 | 'company' => 'alpha_num|nullable', |
175 | 'fieldsMatch' => 'array', |
176 | 'fieldsPattern' => 'array', |
177 | 'exposeContainerLoading' => 'boolean', |
178 | 'autoDebug' => 'boolean', |
179 | 'spa' => 'boolean', |
180 | 'triggers' => 'array' |
181 | ]; |
182 | |
183 | if ($id != null) { |
184 | $rules['_id'] = [ |
185 | 'required', |
186 | Rule::in([$id]) |
187 | ]; |
188 | } |
189 | |
190 | return $request->validate($rules); |
191 | } |
192 | } |
193 |