ldc-cocotte/app/Http/Controllers/Bo/ProductIncosController.php
Jérémy Munsch 74ab9b3041
refactor: reorganize imports across multiple controllers and jobs
This commit refactors the import statements in various controllers and jobs
to improve code organization and readability. Unused imports were removed,
and necessary imports were added to ensure proper functionality.
2026-11-10 18:15:32 +01:00

106 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Bo;
use App\Contracts\Executor;
use App\Cqs\Actions\ProductIncos\AssociateProductInco;
use App\Cqs\Actions\ProductIncos\SyncProductInco;
use App\Cqs\Queries\ProductIncos\GetArcadiaIncoPreviewQuery;
use App\Dto\ProductDto;
use App\Dto\ProductIncoDto;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\ProductIncos\AssociateProductIncoRequest;
use App\Http\Requests\Bo\ProductIncos\PreviewArcadiaIncoRequest;
use App\Http\Requests\Bo\ProductIncos\SyncProductIncoRequest;
use App\Models\Product;
use App\Models\ProductInco;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
class ProductIncosController extends Controller
{
public function __construct(
private readonly Executor $executor,
) {
}
public function preview(PreviewArcadiaIncoRequest $request, Product $product, GetArcadiaIncoPreviewQuery $query): JsonResponse
{
$this->authorize('associateInco', $product);
try {
$preview = $this->executor->execute(
$query,
$request->toGetArcadiaIncoPreviewQueryInput(),
);
} catch (\RuntimeException $e) {
return response()->json([
'message' => __('Arcadia API error: :error', ['error' => $e->getMessage()]),
], 502);
}
if ($preview === null) {
return response()->json([
'message' => __('No INCO sheet found on Arcadia for this article number.'),
], 404);
}
return response()->json($preview);
}
public function associate(AssociateProductIncoRequest $request, Product $product, AssociateProductInco $action): RedirectResponse
{
$this->authorize('associateInco', $product);
try {
$inco = $this->executor->execute(
$action,
$request->toAssociateProductIncoInput($product),
);
} catch (\RuntimeException $e) {
return back()->with('danger', __('Arcadia API error: :error', ['error' => $e->getMessage()]));
}
return to_route('bo.products.incos.edit', compact('product', 'inco'))
->with('success', __('The INCO sheet has been successfully associated with the product.'));
}
public function sync(SyncProductIncoRequest $request, Product $product, SyncProductInco $action): RedirectResponse
{
$this->authorize('syncInco', $product);
try {
$inco = $this->executor->execute(
$action,
$request->toSyncProductIncoInput($product),
);
} catch (\RuntimeException $e) {
return back()->with('danger', __('Arcadia API error: :error', ['error' => $e->getMessage()]));
}
return to_route('bo.products.incos.edit', compact('product', 'inco'))
->with('success', __('The INCO sheet has been successfully synchronized from Arcadia.'));
}
public function create(Product $product): View
{
return $this->edit($product);
}
public function edit(Product $product, ?ProductInco $inco = null): View
{
$this->authorize('associateInco', $product);
$productDto = ProductDto::fromModel($product);
$incoModel = $inco ?? $product->inco;
$incoDto = $incoModel instanceof ProductInco ? ProductIncoDto::fromModel($incoModel) : null;
return view('bo.product-incos.edit', [
'product' => $productDto,
'inco' => $incoDto,
]);
}
}