fix cyclomatic complexity on product update
This commit is contained in:
parent
1d2300ba57
commit
6e359d3c41
1 changed files with 132 additions and 56 deletions
|
|
@ -240,8 +240,6 @@ class ProductsController extends Controller
|
|||
|
||||
$name = $request->name;
|
||||
$description = $request->description;
|
||||
$picture = $request->picture;
|
||||
$picture_large = $request->picture_large;
|
||||
$price = $request->price;
|
||||
$invoicing_unit = $request->invoicing_unit;
|
||||
$priceBuy = $request->price_buy;
|
||||
|
|
@ -260,56 +258,14 @@ class ProductsController extends Controller
|
|||
$slug = Str::slug($name);
|
||||
$rangeId = $request->range_id;
|
||||
$taxRateId = $request->tax_rate_id;
|
||||
$tags = $request->tags;
|
||||
|
||||
// Check si le slug existe déjà dans cette gamme de produits.
|
||||
$slugExist = Product::where([
|
||||
['range_id', $rangeId],
|
||||
['slug', $slug],
|
||||
['id', '<>', $product->id]
|
||||
])->first();
|
||||
$this->checkSlugExistsWithinRange($rangeId, $slug, $product->id, $request);
|
||||
|
||||
if ($slugExist) {
|
||||
return redirect()->back()
|
||||
->withInput($request->input())
|
||||
->withErrors("Erreur : un produit avec le slug d'accès \"" . $slug .
|
||||
"\" éxiste déjà. Veuillez changer le nom du produit.");
|
||||
}
|
||||
|
||||
$str_random = Str::random(5);
|
||||
|
||||
if ($picture !== null) {
|
||||
if ($product->picture !== null && file_exists(public_path('images/produits/') . $product->picture)) {
|
||||
unlink(public_path('images/produits/') . $product->picture);
|
||||
}
|
||||
$imageName = $product->id . '_' . $str_random . '.' . $picture->extension();
|
||||
$picture->move(public_path('images/produits'), $imageName);
|
||||
$product->picture = $imageName;
|
||||
}
|
||||
|
||||
if ($picture_large !== null) {
|
||||
if (
|
||||
$product->picture_large !== null &&
|
||||
file_exists(public_path('images/produits/') . $product->picture_large)
|
||||
) {
|
||||
unlink(public_path('images/produits/') . $product->picture_large);
|
||||
}
|
||||
$imageName = $product->id . '_' . $str_random . '_large.' . $picture_large->extension();
|
||||
$picture_large->move(public_path('images/produits'), $imageName);
|
||||
$product->picture_large = $imageName;
|
||||
}
|
||||
$this->manageProductImages($request->picture, $product, $request->picture_large);
|
||||
|
||||
$product->name = $name;
|
||||
|
||||
if ($product->slug != $slug) {
|
||||
$exist = OldSlug::where('slug', $product->slug)->first();
|
||||
if ($exist === null) {
|
||||
$oldSlug = new OldSlug();
|
||||
$oldSlug->product_id = $product->id;
|
||||
$oldSlug->slug = $product->slug;
|
||||
$oldSlug->save();
|
||||
}
|
||||
}
|
||||
$this->saveNewSlugForProduct($product, $slug);
|
||||
|
||||
$product->description = $description;
|
||||
$product->price = $price;
|
||||
|
|
@ -331,10 +287,7 @@ class ProductsController extends Controller
|
|||
$product->save();
|
||||
|
||||
// Check si le slug existe dans la table old_slug, auquel cas il est attribué a ce nouveau produit.
|
||||
$oldSlug = OldSlug::where('slug', $slug)->first();
|
||||
if ($oldSlug) {
|
||||
$oldSlug->delete();
|
||||
}
|
||||
$this->cleanOldSlug($slug);
|
||||
|
||||
// Ajout des infos additionnelles.
|
||||
$product_type = $request->product_type;
|
||||
|
|
@ -346,6 +299,122 @@ class ProductsController extends Controller
|
|||
}
|
||||
|
||||
// Tags associés.
|
||||
$this->syncTags($request->tags, $product);
|
||||
|
||||
// Produits associés.
|
||||
$this->syncProduitsAssocies($request->prods, $product);
|
||||
|
||||
return redirect()->route('bo.products.edit', $product->id)->with('success', 'Modifications enregistrés !');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if slug already exists, if so redirect
|
||||
*
|
||||
* @param integer $rangeId
|
||||
* @param string $slug
|
||||
* @param integer $productId
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse|void
|
||||
*/
|
||||
private function checkSlugExistsWithinRange(int $rangeId, string $slug, int $productId, Request $request)
|
||||
{
|
||||
// Check si le slug existe déjà dans cette gamme de produits.
|
||||
$slugExist = Product::where([
|
||||
['range_id', $rangeId],
|
||||
['slug', $slug],
|
||||
['id', '<>', $productId]
|
||||
])->first();
|
||||
|
||||
if ($slugExist) {
|
||||
return redirect()->back()
|
||||
->withInput($request->input())
|
||||
->withErrors("Erreur : un produit avec le slug d'accès \"" . $slug .
|
||||
"\" éxiste déjà. Veuillez changer le nom du produit.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If new slug for product then save oldslug
|
||||
*
|
||||
* @param Product $product
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
private function saveNewSlugForProduct(Product $product, string $slug)
|
||||
{
|
||||
if ($product->slug != $slug) {
|
||||
$exist = OldSlug::where('slug', $product->slug)->first();
|
||||
if ($exist === null) {
|
||||
$oldSlug = new OldSlug();
|
||||
$oldSlug->product_id = $product->id;
|
||||
$oldSlug->slug = $product->slug;
|
||||
$oldSlug->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clean oldslug if exists
|
||||
*
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
private function cleanOldSlug(string $slug)
|
||||
{
|
||||
// Check si le slug existe dans la table old_slug, auquel cas il est attribué a ce nouveau produit.
|
||||
$oldSlug = OldSlug::where('slug', $slug)->first();
|
||||
if ($oldSlug) {
|
||||
$oldSlug->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store and update saved images on product
|
||||
*
|
||||
* @param UploadedFile|null $picture
|
||||
* @param Product $product
|
||||
* @param UploadedFile|null $picture_large
|
||||
* @return void
|
||||
*/
|
||||
private function manageProductImages($picture, Product &$product, $picture_large)
|
||||
{
|
||||
|
||||
$str_random = Str::random(5);
|
||||
|
||||
if ($picture !== null) {
|
||||
if (
|
||||
$product->picture !== null &&
|
||||
file_exists(public_path('images/produits/') . $product->picture)
|
||||
) {
|
||||
unlink(public_path('images/produits/') . $product->picture);
|
||||
}
|
||||
$imageName = $product->id . '_' . $str_random . '.' . $picture->extension();
|
||||
$picture->move(public_path('images/produits'), $imageName);
|
||||
$product->picture = $imageName;
|
||||
}
|
||||
|
||||
if ($picture_large !== null) {
|
||||
if (
|
||||
$product->picture_large !== null &&
|
||||
file_exists(public_path('images/produits/') . $product->picture_large)
|
||||
) {
|
||||
unlink(public_path('images/produits/') . $product->picture_large);
|
||||
}
|
||||
$imageName = $product->id . '_' . $str_random . '_large.' . $picture_large->extension();
|
||||
$picture_large->move(public_path('images/produits'), $imageName);
|
||||
$product->picture_large = $imageName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sync tags to product
|
||||
*
|
||||
* @param array $tags
|
||||
* @param Product $product
|
||||
* @return void
|
||||
*/
|
||||
private function syncTags(array $tags, Product &$product)
|
||||
{
|
||||
if (!empty($tags)) {
|
||||
$arTagIds = array();
|
||||
foreach ($tags as $tag) {
|
||||
|
|
@ -362,17 +431,24 @@ class ProductsController extends Controller
|
|||
} else {
|
||||
$product->tags()->sync(array());
|
||||
}
|
||||
}
|
||||
|
||||
// Produits associés.
|
||||
if (!empty($request->prods)) {
|
||||
$productIds = explode(",", $request->prods);
|
||||
/**
|
||||
* sync associated products to product
|
||||
*
|
||||
* @param string $prods
|
||||
* @param Product $product
|
||||
* @return void
|
||||
*/
|
||||
private function syncProduitsAssocies(string $prods, Product $product)
|
||||
{
|
||||
if (!empty($prods)) {
|
||||
$productIds = explode(",", $prods);
|
||||
// Prise en charge de la liaison avec les produits.
|
||||
$product->products()->sync($productIds);
|
||||
} else {
|
||||
$product->products()->sync(array());
|
||||
}
|
||||
|
||||
return redirect()->route('bo.products.edit', $product->id)->with('success', 'Modifications enregistrés !');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue