I'm currently implementing a project and have a question regarding multiple classes that implement the same interface.
So, right now I have multiple discount types that might be applied to an Order. So, I created a DiscountInterface that has the check() method, that checks if the discount is applicable and the apply() method that applies the discount.
So far, so good. Then I implemented the first discount type in a class that implements the DiscountInterface and has the logic for checking and applying this particular discount.
In my controller, I inject the DiscountInterface. When an Order is received I call both the check() and apply() methods and everything is running perfectly.
My question is the following. I to implement a second type of discount. Following my implementation, I will have to create a new class that implements DiscountInterface. But when it's time to call it on the controller how should it be done. Since I have to different classes, with the same methods.
The following code works if I have one class implementing these methods, but what happens if I have two classes implementing them?
public function discount(
Request $request,
DiscountInterface $discount,
CustomerRepository $customer,
ProductRepository $product,
ValidatorInterface $validator,
OrderServiceInterface $orderService
)
{
$data = json_decode($request->getContent(), true);
$order = $orderService->convertDataToOrder($data, $customer, $product, $validator);
if($discount->check($order, $customer)){
$order = $discount->apply($order);
}