- - /home/jeremy/projets/crm/app
+ - /app/srcs/app
- Http
- Controllers
- CustomerController.php
@@ -65,26 +65,14 @@
|
n/a |
0 / 0 |
- 32 |
+ 31 |
|
n/a |
0 / 0 |
- | __construct |
- |
- n/a |
- 0 / 0 |
- |
- n/a |
- 0 / 0 |
- 1 |
- |
-
-
-
- | index |
+ index |
|
n/a |
0 / 0 |
@@ -96,7 +84,7 @@
- | show |
+ show |
|
n/a |
0 / 0 |
@@ -108,7 +96,7 @@
- | delete |
+ delete |
|
n/a |
0 / 0 |
@@ -120,7 +108,7 @@
- | export |
+ export |
|
n/a |
0 / 0 |
@@ -132,7 +120,7 @@
- | exportSubmit |
+ exportSubmit |
|
n/a |
0 / 0 |
@@ -166,306 +154,296 @@
| 15 | use Illuminate\Support\Facades\DB; |
| 16 | use Illuminate\Support\Facades\Schema; |
| 17 | use Illuminate\Support\Facades\Session; |
- | 18 | use Illuminate\Support\Facades\View; |
- | 19 | use Illuminate\Support\Str; |
- | 20 | use League\Csv\Writer; |
- | 21 | use Symfony\Component\HttpFoundation\StreamedResponse; |
- | 22 | |
- | 23 | |
- | 24 | |
- | 25 | |
- | 26 | class CustomerController extends Controller |
- | 27 | { |
- | 28 | |
- | 29 | |
- | 30 | |
- | 31 | public function __construct() |
- | 32 | { |
- | 33 | $this->middleware(function ($request, $next) { |
- | 34 | View::share('modelClass', Customer::class); |
- | 35 | return $next($request); |
- | 36 | }); |
- | 37 | } |
- | 38 | |
- | 39 | |
- | 40 | |
- | 41 | |
- | 42 | |
- | 43 | |
- | 44 | |
- | 45 | public function index(Request $request) |
- | 46 | { |
- | 47 | $modelTable = (new Customer())->getTable(); |
- | 48 | $modelClass = Customer::class; |
- | 49 | $brands = Brand::select('id', 'name')->get(); |
- | 50 | $columns = ['customer_infos.lastname', 'customer_infos.firstname']; |
- | 51 | |
- | 52 | |
- | 53 | |
- | 54 | $query = CustomerInfo::join('customer_logins', function (JoinClause $join) { |
- | 55 | $join->on('customer_logins.brand_id', '=', 'customer_infos.brand_id') |
- | 56 | ->on('customer_logins.customer_id', '=', 'customer_infos.customer_id'); |
- | 57 | }) |
- | 58 | ->join('customers', function (JoinClause $join) { |
- | 59 | $join->on('customers.id', '=', 'customer_infos.customer_id'); |
- | 60 | }) |
- | 61 | ->join('brands', function (JoinClause $join) { |
- | 62 | $join->on('brands.id', '=', 'customer_infos.brand_id'); |
- | 63 | }); |
- | 64 | |
- | 65 | if ($request->rst) { |
- | 66 | Session::remove("crud.$modelTable.brand"); |
- | 67 | Session::remove("crud.$modelTable.search"); |
- | 68 | Session::remove("crud.$modelTable.sort_way"); |
- | 69 | Session::remove("crud.$modelTable.sort_col"); |
- | 70 | } |
- | 71 | |
- | 72 | |
- | 73 | if (request()->brand or Session::has("crud.$modelTable.brand")) { |
- | 74 | $brand = $request->brand ?? Session::get("crud.$modelTable.brand"); |
- | 75 | Session::put("crud.$modelTable.brand", $brand); |
- | 76 | $query = $query->where('customer_infos.brand_id', $brand); |
+ | 18 | use Illuminate\Support\Str; |
+ | 19 | use League\Csv\Writer; |
+ | 20 | use Symfony\Component\HttpFoundation\StreamedResponse; |
+ | 21 | |
+ | 22 | |
+ | 23 | |
+ | 24 | |
+ | 25 | class CustomerController extends Controller |
+ | 26 | { |
+ | 27 | |
+ | 28 | |
+ | 29 | |
+ | 30 | |
+ | 31 | |
+ | 32 | |
+ | 33 | public function index(Request $request) |
+ | 34 | { |
+ | 35 | $modelTable = (new Customer())->getTable(); |
+ | 36 | $modelClass = Customer::class; |
+ | 37 | $brands = Brand::select('id', 'name')->get(); |
+ | 38 | $columns = ['customer_infos.lastname', 'customer_infos.firstname']; |
+ | 39 | |
+ | 40 | |
+ | 41 | |
+ | 42 | $query = CustomerInfo::join('customer_logins', function (JoinClause $join) { |
+ | 43 | $join->on('customer_logins.brand_id', '=', 'customer_infos.brand_id') |
+ | 44 | ->on('customer_logins.customer_id', '=', 'customer_infos.customer_id'); |
+ | 45 | }) |
+ | 46 | ->join('customers', function (JoinClause $join) { |
+ | 47 | $join->on('customers.id', '=', 'customer_infos.customer_id'); |
+ | 48 | }) |
+ | 49 | ->join('brands', function (JoinClause $join) { |
+ | 50 | $join->on('brands.id', '=', 'customer_infos.brand_id'); |
+ | 51 | }); |
+ | 52 | |
+ | 53 | if ($request->rst) { |
+ | 54 | Session::remove("crud.$modelTable.brand"); |
+ | 55 | Session::remove("crud.$modelTable.search"); |
+ | 56 | Session::remove("crud.$modelTable.sort_way"); |
+ | 57 | Session::remove("crud.$modelTable.sort_col"); |
+ | 58 | } |
+ | 59 | |
+ | 60 | if (request()->brand or Session::has("crud.$modelTable.brand")) { |
+ | 61 | $brand = $request->brand ?? Session::get("crud.$modelTable.brand"); |
+ | 62 | Session::put("crud.$modelTable.brand", $brand); |
+ | 63 | $query = $query->where('customer_infos.brand_id', $brand); |
+ | 64 | } |
+ | 65 | |
+ | 66 | |
+ | 67 | if ($request->search or Session::has("crud.$modelTable.search")) { |
+ | 68 | $search = $request->search ?? Session::get("crud.$modelTable.search"); |
+ | 69 | Session::put("crud.$modelTable.search", $search); |
+ | 70 | $query->where(function ($q) use ($columns, $search) { |
+ | 71 | foreach ($columns as $column) { |
+ | 72 | $q = $q->orWhere($column, 'LIKE', "%$search%"); |
+ | 73 | } |
+ | 74 | $q = $q->orWhere('customers.email', 'LIKE', "%$search%"); |
+ | 75 | $q = $q->orWhere('customer_logins.last_connection', 'LIKE', "%$search%"); |
+ | 76 | }); |
| 77 | } |
| 78 | |
- | 79 | |
- | 80 | if ($request->search or Session::has("crud.$modelTable.search")) { |
- | 81 | $search = $request->search ?? Session::get("crud.$modelTable.search"); |
- | 82 | Session::put("crud.$modelTable.search", $search); |
- | 83 | $query->where(function ($q) use ($columns, $search) { |
- | 84 | foreach ($columns as $column) { |
- | 85 | $q = $q->orWhere($column, 'LIKE', "%$search%"); |
- | 86 | } |
- | 87 | $q = $q->orWhere('customers.email', 'LIKE', "%$search%"); |
- | 88 | $q = $q->orWhere('customer_logins.last_connection', 'LIKE', "%$search%"); |
- | 89 | }); |
- | 90 | } |
- | 91 | |
- | 92 | |
- | 93 | if ($request->sort_col) { |
- | 94 | Session::put("crud.$modelTable.sort_col", $request->sort_col); |
- | 95 | } |
- | 96 | if ($request->sort_way) { |
- | 97 | Session::put("crud.$modelTable.sort_way", $request->sort_way); |
- | 98 | } |
- | 99 | |
- | 100 | if ( |
- | 101 | Session::has("crud.$modelTable.sort_col") and |
- | 102 | Session::has("crud.$modelTable.sort_way") and |
- | 103 | (Schema::hasColumn($modelTable, Session::get("crud.$modelTable.sort_col")) or |
- | 104 | in_array(Session::get("crud.$modelTable.sort_col"), [ |
- | 105 | 'brands.name', |
- | 106 | 'customers.email', |
- | 107 | 'customer_infos.firstname', |
- | 108 | 'customer_infos.lastname', |
- | 109 | 'customer_infos.opt_in', |
- | 110 | 'customer_logins.last_connection', |
- | 111 | 'customer_logins.status' |
- | 112 | ])) |
- | 113 | ) { |
- | 114 | $query = $query->orderBy( |
- | 115 | Session::get("crud.$modelTable.sort_col"), |
- | 116 | Session::get("crud.$modelTable.sort_way") |
- | 117 | ); |
- | 118 | } |
- | 119 | |
- | 120 | |
- | 121 | $customers = $query->paginate(config('crud.paginate', 50)); |
- | 122 | |
- | 123 | return view('customer.index', compact( |
- | 124 | 'modelClass', |
- | 125 | 'modelTable', |
- | 126 | 'customers', |
- | 127 | 'brands' |
- | 128 | )); |
- | 129 | } |
- | 130 | |
- | 131 | |
- | 132 | |
- | 133 | |
- | 134 | |
- | 135 | |
- | 136 | |
- | 137 | |
- | 138 | public function show(Customer $customer, Brand $brand) |
- | 139 | { |
- | 140 | $customerInfos = CustomerInfo::where('customer_id', $customer->id) |
- | 141 | ->where('brand_id', $brand->id)->firstOrFail(); |
- | 142 | $customerLogin = CustomerLogin::where('customer_id', $customer->id) |
- | 143 | ->where('brand_id', $brand->id)->first(); |
- | 144 | |
- | 145 | return view('customer.show', compact( |
- | 146 | 'customerInfos', |
- | 147 | 'customerLogin' |
- | 148 | )); |
- | 149 | } |
- | 150 | |
- | 151 | |
- | 152 | |
- | 153 | |
- | 154 | |
- | 155 | |
- | 156 | |
- | 157 | |
- | 158 | public function delete(Customer $customer, Brand $brand) |
- | 159 | { |
- | 160 | try { |
- | 161 | |
- | 162 | |
- | 163 | $customerInfo = $customer->info($brand, Customer::FIRST_OR_FAIL); |
- | 164 | |
- | 165 | $customerLogin = $customer->login($brand, Customer::FIRST_OR_FAIL); |
- | 166 | |
- | 167 | DB::transaction(function () use ( |
- | 168 | $brand, |
- | 169 | $customer, |
- | 170 | $customerLogin, |
- | 171 | $customerInfo |
- | 172 | ) { |
- | 173 | if ( |
- | 174 | $customerInfo->delete() === false || |
- | 175 | $customerLogin->delete() === false |
- | 176 | ) { |
- | 177 | throw new \ErrorException('Api failed to delete customer.'); |
- | 178 | }; |
- | 179 | |
- | 180 | $customer->optInHistory()->whereHas('brand', function ($q) use ($brand) { |
- | 181 | $q->where('id', $brand->id); |
- | 182 | })->delete(); |
- | 183 | |
- | 184 | $logins = $customer->logins()->count(); |
- | 185 | $infos = $customer->infos()->count(); |
- | 186 | $history = $customer->optInHistory()->count(); |
- | 187 | |
- | 188 | |
- | 189 | if (!$logins and !$infos and !$history) { |
- | 190 | if ($customer->delete() === false) { |
- | 191 | throw new \ErrorException('Api failed to update customer.'); |
- | 192 | } |
- | 193 | } |
- | 194 | }); |
- | 195 | } catch (\ErrorException $e) { |
- | 196 | return redirect()->route('customers.index') |
- | 197 | ->with('error', trans('Le compte n\'as pas pu être supprimé.')); |
- | 198 | } |
- | 199 | |
- | 200 | return redirect()->route('customers.index')->with('warning', trans('Compte supprimé.')); |
- | 201 | } |
- | 202 | |
- | 203 | |
- | 204 | |
- | 205 | |
- | 206 | |
- | 207 | |
- | 208 | public function export() |
- | 209 | { |
- | 210 | $brands = Brand::all(); |
- | 211 | return view('customer.export', compact( |
- | 212 | 'brands' |
- | 213 | )); |
- | 214 | } |
- | 215 | |
- | 216 | |
- | 217 | |
- | 218 | |
- | 219 | |
- | 220 | |
- | 221 | |
- | 222 | |
- | 223 | public function exportSubmit(CustomerExport $request) |
- | 224 | { |
- | 225 | $pagination = 2000; |
- | 226 | $header = '"lastname";"firstname";"email";"hashcode"'; |
- | 227 | |
- | 228 | $w = Writer::createFromString($header . PHP_EOL); |
- | 229 | $w->setDelimiter(';'); |
- | 230 | $w->setEnclosure('"'); |
- | 231 | $w->setOutputBOM(Writer::BOM_UTF8); |
- | 232 | |
- | 233 | $exportType = CustomerExportType::make(\intval($request->export_type)); |
- | 234 | |
- | 235 | |
- | 236 | $response = new StreamedResponse(); |
- | 237 | $response->headers->set('Content-Encoding', 'none'); |
- | 238 | $response->headers->set('Content-Type', 'text/csv; charset=UTF-8'); |
- | 239 | $response->headers->set('Description', 'File Transfer'); |
- | 240 | $response->headers->set( |
- | 241 | 'Content-Disposition', |
- | 242 | sprintf( |
- | 243 | 'attachment; filename="Export pour newsletter %s %s.csv"', |
- | 244 | Carbon::now()->format('d-m-Y_H-i'), |
- | 245 | Str::slug($exportType) |
- | 246 | ) |
- | 247 | ); |
- | 248 | |
- | 249 | switch ($exportType) { |
- | 250 | case CustomerExportType::both(): |
- | 251 | $continue = true; |
- | 252 | |
- | 253 | case CustomerExportType::customer(): |
- | 254 | $page = 1; |
- | 255 | |
- | 256 | $req = CustomerInfo::where('brand_id', $request->brand) |
- | 257 | ->where('opt_in', true); |
- | 258 | $total = $req->count(); |
- | 259 | $pages = \ceil($total / $pagination); |
- | 260 | do { |
- | 261 | |
- | 262 | $items = $req->with('customer', function ($q) { |
- | 263 | $q->select(['id', 'email']); |
- | 264 | })->paginate( |
- | 265 | $pagination, |
- | 266 | ['lastname', 'firstname', 'hashcode', 'customer_id'], |
- | 267 | 'page', |
- | 268 | $page |
- | 269 | ); |
- | 270 | $items->map(function (CustomerInfo $customerInfo) use ($w) { |
- | 271 | $w->insertOne([ |
- | 272 | 'lastname' => $customerInfo->lastname, |
- | 273 | 'firstname' => $customerInfo->firstname, |
- | 274 | 'email' => $customerInfo->customer->email, |
- | 275 | 'hashcode' => $customerInfo->hashcode |
- | 276 | ]); |
- | 277 | }); |
- | 278 | } while ($page++ <= $pages); |
- | 279 | if (!isset($continue)) { |
- | 280 | break; |
- | 281 | } |
- | 282 | |
- | 283 | case CustomerExportType::singleOptin(): |
- | 284 | SingleOptin::where('brand_id', $request->brand)->get() |
- | 285 | ->map(function (SingleOptin $singleOptin) use ($w) { |
- | 286 | $w->insertOne([ |
- | 287 | 'lastname' => '', |
- | 288 | 'firstname' => '', |
- | 289 | 'email' => $singleOptin->email, |
- | 290 | 'hashcode' => '' |
- | 291 | ]); |
- | 292 | }); |
- | 293 | break; |
- | 294 | default: |
- | 295 | throw new \Exception(sprintf('Unhandled CustomerExportType %s', $exportType->label)); |
- | 296 | } |
- | 297 | |
- | 298 | |
- | 299 | |
- | 300 | |
- | 301 | |
- | 302 | |
- | 303 | |
- | 304 | |
- | 305 | $flush_threshold = 1000; |
- | 306 | |
- | 307 | $response->setCallback(function () use ($w, $flush_threshold) { |
- | 308 | foreach ($w->chunk(1024) as $offset => $chunk) { |
- | 309 | echo $chunk; |
- | 310 | if ($offset % $flush_threshold === 0) { |
- | 311 | flush(); |
- | 312 | } |
- | 313 | } |
- | 314 | }); |
- | 315 | return $response; |
- | 316 | } |
- | 317 | } |
+ | 79 | |
+ | 80 | if ($request->sort_col) { |
+ | 81 | Session::put("crud.$modelTable.sort_col", $request->sort_col); |
+ | 82 | } |
+ | 83 | if ($request->sort_way) { |
+ | 84 | Session::put("crud.$modelTable.sort_way", $request->sort_way); |
+ | 85 | } |
+ | 86 | |
+ | 87 | if ( |
+ | 88 | Session::has("crud.$modelTable.sort_col") and |
+ | 89 | Session::has("crud.$modelTable.sort_way") and |
+ | 90 | (Schema::hasColumn($modelTable, Session::get("crud.$modelTable.sort_col")) or |
+ | 91 | in_array(Session::get("crud.$modelTable.sort_col"), [ |
+ | 92 | 'brands.name', |
+ | 93 | 'customers.email', |
+ | 94 | 'customer_infos.firstname', |
+ | 95 | 'customer_infos.lastname', |
+ | 96 | 'customer_infos.opt_in', |
+ | 97 | 'customer_logins.last_connection', |
+ | 98 | 'customer_logins.status' |
+ | 99 | ])) |
+ | 100 | ) { |
+ | 101 | $query = $query->orderBy( |
+ | 102 | Session::get("crud.$modelTable.sort_col"), |
+ | 103 | Session::get("crud.$modelTable.sort_way") |
+ | 104 | ); |
+ | 105 | } |
+ | 106 | |
+ | 107 | $searchFields = trans('Email, Prénom, Nom'); |
+ | 108 | |
+ | 109 | |
+ | 110 | $customerModels = $query->paginate(50); |
+ | 111 | |
+ | 112 | return view('customers.index', compact( |
+ | 113 | 'modelClass', |
+ | 114 | 'modelTable', |
+ | 115 | 'customerModels', |
+ | 116 | 'brands', |
+ | 117 | 'searchFields' |
+ | 118 | )); |
+ | 119 | } |
+ | 120 | |
+ | 121 | |
+ | 122 | |
+ | 123 | |
+ | 124 | |
+ | 125 | |
+ | 126 | |
+ | 127 | |
+ | 128 | public function show(Customer $customer, Brand $brand) |
+ | 129 | { |
+ | 130 | $customerInfos = CustomerInfo::where('customer_id', $customer->id) |
+ | 131 | ->where('brand_id', $brand->id)->firstOrFail(); |
+ | 132 | $customerLogin = CustomerLogin::where('customer_id', $customer->id) |
+ | 133 | ->where('brand_id', $brand->id)->first(); |
+ | 134 | |
+ | 135 | return view('customers.show', compact( |
+ | 136 | 'customerInfos', |
+ | 137 | 'customerLogin' |
+ | 138 | )); |
+ | 139 | } |
+ | 140 | |
+ | 141 | |
+ | 142 | |
+ | 143 | |
+ | 144 | |
+ | 145 | |
+ | 146 | |
+ | 147 | |
+ | 148 | public function delete(Customer $customer, Brand $brand) |
+ | 149 | { |
+ | 150 | try { |
+ | 151 | |
+ | 152 | |
+ | 153 | $customerInfo = $customer->info($brand, Customer::FIRST_OR_FAIL); |
+ | 154 | |
+ | 155 | $customerLogin = $customer->login($brand, Customer::FIRST_OR_FAIL); |
+ | 156 | |
+ | 157 | DB::transaction(function () use ( |
+ | 158 | $brand, |
+ | 159 | $customer, |
+ | 160 | $customerLogin, |
+ | 161 | $customerInfo |
+ | 162 | ) { |
+ | 163 | if ( |
+ | 164 | $customerInfo->delete() === false || |
+ | 165 | $customerLogin->delete() === false |
+ | 166 | ) { |
+ | 167 | throw new \ErrorException('Api failed to delete customer.'); |
+ | 168 | }; |
+ | 169 | |
+ | 170 | $customer->optInHistory()->whereHas('brand', function ($q) use ($brand) { |
+ | 171 | $q->where('id', $brand->id); |
+ | 172 | })->delete(); |
+ | 173 | |
+ | 174 | $logins = $customer->logins()->count(); |
+ | 175 | $infos = $customer->infos()->count(); |
+ | 176 | $history = $customer->optInHistory()->count(); |
+ | 177 | |
+ | 178 | |
+ | 179 | if (!$logins and !$infos and !$history) { |
+ | 180 | if ($customer->delete() === false) { |
+ | 181 | throw new \ErrorException('Api failed to update customer.'); |
+ | 182 | } |
+ | 183 | } |
+ | 184 | }); |
+ | 185 | } catch (\ErrorException $e) { |
+ | 186 | return redirect()->route('customers.index') |
+ | 187 | ->with('error', trans('Le compte n\'a pas pu être supprimé.')); |
+ | 188 | } |
+ | 189 | |
+ | 190 | return redirect()->route('customers.index')->with('warning', trans('Compte supprimé.')); |
+ | 191 | } |
+ | 192 | |
+ | 193 | |
+ | 194 | |
+ | 195 | |
+ | 196 | |
+ | 197 | |
+ | 198 | public function export() |
+ | 199 | { |
+ | 200 | $brands = Brand::all(); |
+ | 201 | return view('customers.export', compact( |
+ | 202 | 'brands' |
+ | 203 | )); |
+ | 204 | } |
+ | 205 | |
+ | 206 | |
+ | 207 | |
+ | 208 | |
+ | 209 | |
+ | 210 | |
+ | 211 | |
+ | 212 | |
+ | 213 | public function exportSubmit(CustomerExport $request) |
+ | 214 | { |
+ | 215 | $pagination = 2000; |
+ | 216 | $header = '"lastname";"firstname";"email";"hashcode"'; |
+ | 217 | |
+ | 218 | $w = Writer::createFromString($header . PHP_EOL); |
+ | 219 | $w->setDelimiter(';'); |
+ | 220 | $w->setEnclosure('"'); |
+ | 221 | $w->setOutputBOM(Writer::BOM_UTF8); |
+ | 222 | |
+ | 223 | $exportType = CustomerExportType::make(\intval($request->export_type)); |
+ | 224 | |
+ | 225 | |
+ | 226 | $response = new StreamedResponse(); |
+ | 227 | $response->headers->set('Content-Encoding', 'none'); |
+ | 228 | $response->headers->set('Content-Type', 'text/csv; charset=UTF-8'); |
+ | 229 | $response->headers->set('Description', 'File Transfer'); |
+ | 230 | $response->headers->set( |
+ | 231 | 'Content-Disposition', |
+ | 232 | sprintf( |
+ | 233 | 'attachment; filename="Export pour newsletter %s %s.csv"', |
+ | 234 | Carbon::now()->format('d-m-Y_H-i'), |
+ | 235 | Str::slug($exportType) |
+ | 236 | ) |
+ | 237 | ); |
+ | 238 | |
+ | 239 | switch ($exportType) { |
+ | 240 | case CustomerExportType::both(): |
+ | 241 | $continue = true; |
+ | 242 | |
+ | 243 | case CustomerExportType::customer(): |
+ | 244 | $page = 1; |
+ | 245 | |
+ | 246 | $req = CustomerInfo::where('brand_id', $request->brand) |
+ | 247 | ->where('opt_in', true); |
+ | 248 | $total = $req->count(); |
+ | 249 | $pages = \ceil($total / $pagination); |
+ | 250 | do { |
+ | 251 | |
+ | 252 | $items = $req->with('customer', function ($q) { |
+ | 253 | $q->select(['id', 'email']); |
+ | 254 | })->paginate( |
+ | 255 | $pagination, |
+ | 256 | ['lastname', 'firstname', 'hashcode', 'customer_id'], |
+ | 257 | 'page', |
+ | 258 | $page |
+ | 259 | ); |
+ | 260 | $items->map(function (CustomerInfo $customerInfo) use ($w) { |
+ | 261 | $w->insertOne([ |
+ | 262 | 'lastname' => $customerInfo->lastname, |
+ | 263 | 'firstname' => $customerInfo->firstname, |
+ | 264 | 'email' => $customerInfo->customer->email, |
+ | 265 | 'hashcode' => $customerInfo->hashcode |
+ | 266 | ]); |
+ | 267 | }); |
+ | 268 | } while ($page++ <= $pages); |
+ | 269 | if (!isset($continue)) { |
+ | 270 | break; |
+ | 271 | } |
+ | 272 | |
+ | 273 | case CustomerExportType::singleOptin(): |
+ | 274 | SingleOptin::where('brand_id', $request->brand)->get() |
+ | 275 | ->map(function (SingleOptin $singleOptin) use ($w) { |
+ | 276 | $w->insertOne([ |
+ | 277 | 'lastname' => '', |
+ | 278 | 'firstname' => '', |
+ | 279 | 'email' => $singleOptin->email, |
+ | 280 | 'hashcode' => '' |
+ | 281 | ]); |
+ | 282 | }); |
+ | 283 | break; |
+ | 284 | default: |
+ | 285 | throw new \Exception(sprintf('Unhandled CustomerExportType %s', $exportType->label)); |
+ | 286 | } |
+ | 287 | |
+ | 288 | |
+ | 289 | |
+ | 290 | |
+ | 291 | |
+ | 292 | |
+ | 293 | |
+ | 294 | |
+ | 295 | $flush_threshold = 1000; |
+ | 296 | |
+ | 297 | $response->setCallback(function () use ($w, $flush_threshold) { |
+ | 298 | foreach ($w->chunk(1024) as $offset => $chunk) { |
+ | 299 | echo $chunk; |
+ | 300 | if ($offset % $flush_threshold === 0) { |
+ | 301 | flush(); |
+ | 302 | } |
+ | 303 | } |
+ | 304 | }); |
+ | 305 | return $response; |
+ | 306 | } |
+ | 307 | } |