Skip to content

Instantly share code, notes, and snippets.

@samadfcibd
Last active July 18, 2020 16:46
Show Gist options
  • Save samadfcibd/2904349c7d139a96c6b886176987db23 to your computer and use it in GitHub Desktop.
Save samadfcibd/2904349c7d139a96c6b886176987db23 to your computer and use it in GitHub Desktop.
<?php
interface paymentInterface
{
public function pay();
}
class payWithPaypal implements paymentInterface
{
public function pay()
{
// TODO: Implement pay() method.
}
}
class payWithCreditCard implements paymentInterface
{
public function pay()
{
// TODO: Implement pay() method.
}
}
class payWithWireTransfer implements paymentInterface
{
public function pay()
{
// TODO: Implement pay() method.
}
}
class PaymentService
{
public function initialize($payment_type)
{
switch ($payment_type) {
case 'Paypal':
return new payWithPaypal();
break;
case 'Credit Card':
return new payWithCreditCard();
break;
default:
return new payWithWireTransfer();
}
}
}
class PaymentController
{
public function pay(Request $request, PaymentService $paymentService)
{
$payment_type = $request->input('payment_type');
// OCP refactor
$payment = $paymentService->initialize('Paypal');
$payment->pay();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment