<?php
namespace front\Controllers;

use Domain\Coupon\CouponRepository;

class ShopCouponController
{
  private CouponRepository $repository;

  public function __construct( CouponRepository $repository )
  {
    $this->repository = $repository;
  }

  public function useCoupon()
  {
    $coupon = $this->repository->findByName( (string)\Shared\Helpers\Helpers::get( 'coupon' ) );

    if ( $coupon && $this->repository->isAvailable( $coupon ) )
      \Shared\Helpers\Helpers::set_session( 'coupon', $coupon );
    else
      \Shared\Helpers\Helpers::alert( 'Podany kod rabatowy jest nieprawidłowy.' );

    header( 'Location: /koszyk' );
    exit;
  }

  public function deleteCoupon()
  {
    \Shared\Helpers\Helpers::delete_session( 'coupon' );
    header( 'Location: /koszyk' );
    exit;
  }
}
