Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
92.31% covered (success)
92.31%
12 / 13
CRAP
98.48% covered (success)
98.48%
65 / 66
BrandController
0.00% covered (danger)
0.00%
0 / 1
92.31% covered (success)
92.31%
12 / 13
19
98.48% covered (success)
98.48%
65 / 66
 index
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 store
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
 show
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 update
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 destroy
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 validateData
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 updateCampaigns
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 addCampaign
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 delCampaign
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 updateDomains
0.00% covered (danger)
0.00%
0 / 1
4.01
91.67% covered (success)
91.67%
11 / 12
 updateSite
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 addSite
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 delSite
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php
2
3namespace Qmp\Laravel\Brand\Controllers;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Response;
7use Illuminate\Http\Request;
8use Illuminate\Support\Facades\Log;
9use Illuminate\Support\Str;
10use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController;
11use Illuminate\Validation\Rule;
12
13use Qmp\Laravel\Brand\Models\Brand;
14use Qmp\Laravel\Brand\Models\Domain;
15
16
17use Qmp\Laravel\MicroService\Client\Client;
18use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest;
19
20class BrandController extends AbstractMicroServiceController
21{
22    /**
23     * Display a listing of the resource.
24     *
25     * @return \Illuminate\Http\Response
26     */
27    public function index()
28    {
29        return Brand::all();
30    }
31
32    /**
33     * Store a newly created resource in storage.
34     *
35     * @param  \Illuminate\Http\Request  $request
36     * @return \Illuminate\Http\Response
37     */
38    public function store(Request $request)
39    {
40        $validatedData = $this->validateData($request);
41        $data = $request->only(['name', 'campaign_ids']);
42        $data['hash'] = Str::random(32);
43        
44        $brand = Brand::create($data);
45
46        $this->updateDomains($brand->id, $request->domains);
47
48        $this->updateCampaigns($brand);
49
50        return $brand;
51    }
52
53    /**
54     * Display the specified resource.
55     *
56     * @return \Illuminate\Http\Response
57     */
58    public function show($configId)
59    {
60        return Brand::findOrFail($configId);
61    }
62
63    /**
64     * Update the specified resource in storage.
65     *
66     * @param  \Illuminate\Http\Request  $request
67     * @return \Illuminate\Http\Response
68     */
69    public function update(Request $request, $id)
70    {
71        $validatedData = $this->validateData($request, $id);
72        $model = Brand::findOrFail($id);
73
74        $result = $model->update($validatedData);
75
76        $this->updateDomains($model->id, $request->domains);
77
78        $this->updateCampaigns($model);
79
80        return response()->json(['status' => $result ? 'ok' : 'ko']);
81    }
82
83    /**
84     * Remove the specified resource from storage.
85     *
86     * @return \Illuminate\Http\Response
87     */
88    public function destroy($id)
89    {
90        $brand = Brand::find($id);
91        Domain::where('brand_id', $id)->update(['brand_id' => null]);
92        $brand->campaign_ids = [];
93        $this->updateCampaigns($brand);
94        
95        $brand->delete();
96
97        return response()->json(['status' => 'ok']);
98    }
99
100    protected function validateData(Request $request, $id = null)
101    {
102        $rules =[
103            'name' => 'required|string',
104            'domains' => 'present|array',
105            'campaign_ids' => 'present|array'
106        ];
107
108        if ($id != null) {
109            $rules['id'] = [
110                'required',
111                Rule::in([$id])
112            ];
113        }
114
115        return $request->validate($rules);
116    }
117
118    protected function updateCampaigns(Brand $brand)
119    {
120        $request = ClientRequest::createObject('service_campaigns', 'campaign/update-brand', ['body' => ['brand_id' => $brand->id, 'campaign_ids' => $brand->campaign_ids]]);
121        $response = Client::systemSend('post', $request);               
122    }
123
124    public function addCampaign(Request $request, $brandId, $campaignId)
125    {
126        $brand = Brand::findOrFail($brandId);
127        $brandCampaignIds = $brand->campaign_ids;
128        $brandCampaignIds[] = $campaignId;
129        $brand->campaign_ids = array_filter(array_unique($brandCampaignIds), function($campaignId) {
130            return strlen($campaignId) > 0;
131        });
132        $brand->save();
133
134        return response()->json(['status' => 'ok']);
135    }
136
137    public function delCampaign(Request $request, $brandId, $campaignId)
138    {
139        $brand = Brand::findOrFail($brandId);
140        $brandCampaignIds = $brand->campaign_ids;
141        
142        if (($key = array_search($campaignId, $brandCampaignIds)) !== false) {
143            unset($brandCampaignIds[$key]);
144        }
145
146        $brand->campaign_ids = $brandCampaignIds;
147
148        $brand->save();
149
150        return response()->json(['status' => 'ok']);
151    }
152
153    protected function updateDomains($brandId, $domains)
154    {
155        $domainsToRemove = Domain::where('brand_id', $brandId)->get()->pluck('name')->toArray();
156        
157        Domain::where('brand_id', $brandId)->delete();
158        foreach($domains as $domain) {
159            if (($key = array_search($domain, $domainsToRemove)) !== false) {
160                unset($domainsToRemove[$key]);
161            }
162            $this->updateSite($brandId, $domain);
163
164            $domain = Domain::firstOrCreate(['name' => $domain]);
165            $domain->brand_id = $brandId;
166            $domain->save();
167        }
168
169        foreach ($domainsToRemove as $domain) {
170            $this->updateSite(null, $domain);
171        }
172    }    
173
174    protected function updateSite($brandId, $siteId)
175    {
176        $request = ClientRequest::createObject('service_site', "site/$siteId/update-brand", ['body' => ['brand_id' => $brandId]]);
177        $response = Client::systemSend('post', $request);
178    }
179
180    public function addSite($brandId, $siteId)
181    {
182        $domain = Domain::firstOrCreate(['name' => $siteId]);
183        $domain->brand_id = $brandId;
184        $domain->save();
185
186        return response()->json(['status' => 'ok']);
187    }
188
189    public function delSite($brandId, $siteId)
190    {
191        Domain::where('name', $siteId)->delete();
192
193        return response()->json(['status' => 'ok']);
194    }
195}