Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Jul 23, 2024
1 parent e91c21f commit 3d1752d
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 81 deletions.
4 changes: 3 additions & 1 deletion src/Console/Commands/AbandonedCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use EscolaLms\Cart\Events\AbandonedCartEvent;
use EscolaLms\Cart\Models\Cart;
use EscolaLms\Cart\Services\Contracts\ShopServiceContract;
use Illuminate\Console\Command;

Expand All @@ -21,9 +22,10 @@ public function __construct(ShopServiceContract $shopService)
$this->shopService = $shopService;
}

public function handle()
public function handle(): void
{
$abandonedCarts = $this->shopService->getAbandonedCarts(Carbon::now()->subHours(24), Carbon::now());
/** @var Cart $abandonedCart */
foreach ($abandonedCarts as $abandonedCart) {
event(new AbandonedCartEvent($abandonedCart));
}
Expand Down
5 changes: 3 additions & 2 deletions src/Dtos/ProductsSearchDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class ProductsSearchDto implements DtoContract
protected ?string $type;
protected ?bool $free;
protected ?string $productable_class;
protected $productable_id;
protected ?int $productable_id;
protected ?string $productable_type;
protected ?bool $purchasable = true;
protected ?int $per_page;
protected ?array $tags;

public function __construct(?string $name = null, ?string $type = null, ?bool $free = null, ?string $productable_type = null, ?int $productable_id = null, ?bool $purchasable = true, ?int $per_page = null, ?array $tags)
public function __construct(?string $name = null, ?string $type = null, ?bool $free = null, ?string $productable_type = null, ?int $productable_id = null, ?bool $purchasable = true, ?int $per_page = null, ?array $tags = null)
{
$this->name = $name;
$this->type = $type;
Expand Down
6 changes: 3 additions & 3 deletions src/EscolaLmsCartServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EscolaLmsCartServiceProvider extends ServiceProvider
ShopServiceContract::class => ShopService::class,
];

public function boot()
public function boot(): void
{
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
Expand All @@ -41,7 +41,7 @@ public function boot()
}
}

public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/config.php', 'escolalms_cart');

Expand All @@ -50,7 +50,7 @@ public function register()
$this->app->register(SettingsServiceProvider::class);
$this->app->register(ScheduleServiceProvider::class);

if (!$this->app->getProviders(EscolaLms\Cart\EscolaLmsTemplatesServiceProvider::class)) {
if (!$this->app->getProviders(EscolaLmsTemplatesServiceProvider::class)) {
$this->app->register(EscolaLmsTemplatesServiceProvider::class);
}
if (!$this->app->getProviders(TreestoneitCartServiceProvider::class)) {
Expand Down
14 changes: 11 additions & 3 deletions src/Http/Controllers/CartApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use EscolaLms\Cart\Services\Contracts\ProductServiceContract;
use EscolaLms\Cart\Services\Contracts\ShopServiceContract;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

Expand All @@ -26,13 +27,16 @@ public function __construct(ProductServiceContract $productService, ShopServiceC

public function index(Request $request): JsonResponse
{
$cart = $this->shopService->cartForUser($request->user());
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
return $this->sendResponseForResource($this->shopService->cartAsJsonResource($cart), __("Cart data fetched"));
}

public function setProductQuantity(ProductSetQuantityInCartRequest $request): JsonResponse
{
$product = $request->getProduct();
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
if (!$this->productService->productIsBuyableByUser($product, $user, false, $request->getQuantity())) {
Expand All @@ -46,6 +50,7 @@ public function setProductQuantity(ProductSetQuantityInCartRequest $request): Js

public function addMissingProducts(AddMissingProductsRequest $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
$this->shopService->addMissingProductsToCart($cart, $request->input('products', []));
Expand All @@ -59,8 +64,10 @@ public function addProductable(ProductableAddToCartRequest $request): JsonRespon
if (!$product) {
return $this->sendError(__('Single Product for this productable does not exist'), 404);
}
$cart = $this->shopService->cartForUser($request->user());
if (!$this->productService->productIsBuyableByUser($product, $request->user())) {
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
if (!$this->productService->productIsBuyableByUser($product, $user)) {
return $this->sendError(__("You can not add this product to cart"), 403);
}
$this->shopService->addProductToCart($cart, $product, 1);
Expand All @@ -70,6 +77,7 @@ public function addProductable(ProductableAddToCartRequest $request): JsonRespon
public function remove(ProductRemoveFromCartRequest $request): JsonResponse
{
$product = $request->getProduct();
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);
return $this->sendResponse(
Expand Down
9 changes: 7 additions & 2 deletions src/Http/Controllers/PaymentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EscolaLms\Cart\Http\Swagger\PaymentSwagger;
use EscolaLms\Cart\Services\Contracts\ShopServiceContract;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use EscolaLms\Payments\Http\Resources\PaymentResource;
use Illuminate\Http\JsonResponse;

Expand All @@ -22,7 +23,9 @@ public function __construct(ShopServiceContract $shopService)
public function pay(PaymentCartRequest $request): JsonResponse
{
try {
$cart = $this->shopService->cartForUser($request->user());
/** @var User $user */
$user = $request->user();
$cart = $this->shopService->cartForUser($user);

$payment = $this->shopService->purchaseCart(
$cart,
Expand All @@ -39,9 +42,11 @@ public function pay(PaymentCartRequest $request): JsonResponse
public function payProduct(PaymentProductRequest $request): JsonResponse
{
try {
/** @var User $user */
$user = $request->user();
$payment = $this->shopService->purchaseProduct(
$request->getProduct(),
$request->user(),
$user,
$request->toClientDetailsDto(),
$request->getAdditionalPaymentParameters()
);
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Controllers/ProductApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use EscolaLms\Cart\Services\Contracts\ProductServiceContract;
use EscolaLms\Cart\Services\Contracts\ShopServiceContract;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use Illuminate\Http\JsonResponse;
use EscolaLms\Core\Dtos\OrderDto as SortDto;

Expand Down Expand Up @@ -48,7 +49,9 @@ public function indexMy(ProductSearchMyRequest $request): JsonResponse

public function cancel(ProductRecursiveCancelRequest $request): JsonResponse
{
$this->productService->cancelActiveRecursiveProduct($request->getProduct(), $request->user());
/** @var User $user */
$user = $request->user();
$this->productService->cancelActiveRecursiveProduct($request->getProduct(), $user);

return $this->sendSuccess(__('Subscription cancelled successfully'));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/ProductablesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EscolaLms\Cart\Http\Swagger\ProductablesSwagger;
use EscolaLms\Cart\Services\Contracts\ProductServiceContract;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use Illuminate\Http\JsonResponse;

class ProductablesApiController extends EscolaLmsBaseController implements ProductablesSwagger
Expand All @@ -20,6 +21,7 @@ public function __construct(ProductServiceContract $productService)

public function attach(ProductableAttachRequest $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$activeSubscription = $this->productService->hasActiveSubscriptionAllIn($user);

Expand Down
12 changes: 8 additions & 4 deletions src/Http/Requests/Admin/ProductAttachRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class ProductAttachRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return Gate::allows('attach', $this->getProduct());
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
Expand All @@ -33,12 +33,16 @@ public function rules(): array

public function getProductId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getProduct(): Product
{
return Product::findOrFail($this->getProductId());
/** @var Product $product */
$product = Product::findOrFail($this->getProductId());
return $product;
}

public function getCartUser(): User
Expand Down
12 changes: 8 additions & 4 deletions src/Http/Requests/Admin/ProductDeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

class ProductDeleteRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return Gate::allows('delete', $this->getProduct());
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
Expand All @@ -31,11 +31,15 @@ public function rules(): array

public function getId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getProduct(): Product
{
return Product::findOrFail($this->getId());
/** @var Product $product */
$product = Product::findOrFail($this->getId());
return $product;
}
}
12 changes: 8 additions & 4 deletions src/Http/Requests/Admin/ProductDetachRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class ProductDetachRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return Gate::allows('detach', $this->getProduct());
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
Expand All @@ -33,12 +33,16 @@ public function rules(): array

public function getProductId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getProduct(): Product
{
return Product::findOrFail($this->getProductId());
/** @var Product $product */
$product = Product::findOrFail($this->getProductId());
return $product;
}

public function getCartUser(): User
Expand Down
9 changes: 6 additions & 3 deletions src/Http/Requests/Admin/ProductReadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class ProductReadRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return Gate::allows('view', $this->getProduct());
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
Expand All @@ -32,11 +32,14 @@ public function rules(): array

public function getId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getProduct(): Product
{
/** @var Product|null $product */
$product = Product::find($this->getId());

if ($product === null) {
Expand Down
8 changes: 6 additions & 2 deletions src/Http/Requests/Admin/ProductUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ public function rules(): array

public function getId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getProduct(): Product
{
return Product::findOrFail($this->getId());
/** @var Product $product */
$product = Product::findOrFail($this->getId());
return $product;
}
}
8 changes: 5 additions & 3 deletions src/Http/Requests/CartItemRemoveFromCartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

class CartItemRemoveFromCartRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return !!$this->user();
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
Expand All @@ -30,6 +30,8 @@ public function rules(): array

public function getCartItemId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}
}
11 changes: 7 additions & 4 deletions src/Http/Requests/OrderViewRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@

class OrderViewRequest extends FormRequest
{
public function authorize()
public function authorize(): bool
{
return Gate::allows('view', $this->getOrder());
}

protected function prepareForValidation()
protected function prepareForValidation(): void
{
parent::prepareForValidation();
$this->merge([
'id' => $this->route('id')
]);
}

public function rules()
public function rules(): array
{
return [
'id' => ['required', 'integer', Rule::exists(Order::class, 'id')],
Expand All @@ -32,11 +32,14 @@ public function rules()

public function getId(): int
{
return $this->route('id');
/** @var int $id */
$id = $this->route('id');
return $id;
}

public function getOrder(): Order
{
/** @var Order|null $order */
$order = Order::find($this->getId());

if (is_null($order)) {
Expand Down
Loading

0 comments on commit 3d1752d

Please sign in to comment.