Skip to content

Instantly share code, notes, and snippets.

@vimkaf
Last active February 1, 2023 17:13
Show Gist options
  • Save vimkaf/ea1ea6fedef974649d9fb0eea3bf6485 to your computer and use it in GitHub Desktop.
Save vimkaf/ea1ea6fedef974649d9fb0eea3bf6485 to your computer and use it in GitHub Desktop.
Understanding Adapter Design Pattern
<?php
interface BankAccounts{
public function createBankAccount();
}
interface PaymentGateways extends BankAccounts
{
public function processpayment(float|int $amount);
public function createAccount(string $name);
}
class Paystack implements PaymentGateways
{
public $name = 'Paystack';
public function __construct()
{
}
function createBankAccount()
{
// TODO: Implement createBankAccount() method.
}
public function processpayment(float|int $amount)
{
// TODO: Implement processpayment() method.
}
public function createAccount(string $name)
{
return "{$name} has been created";
}
}
class PaymentAdapter
{
private $paymentGateway;
public function __construct(PaymentGateways $paymentGateway)
{
$this->paymentGateway = $paymentGateway;
}
public function processPayment($amount) {
$response = $this->paymentGateway->processPayment($amount);
return array('status' => $response['status'], 'transaction_id' => $response['transaction_id'], 'payment_method' => get_class($this->paymentGateway));
}
public function createAccount(string $accountName){
return $this->paymentGateway->createAccount($accountName);
}
}
class TestController extends Controller{
private $paymentAdapter;
public function __construct(PaymentAdapter $paymentAdapter) {
$this->paymentAdapter = $paymentAdapter;
}
public function createAccount($name) {
$this->paymentAdapter = new PaymentAdapter(new Paystack());
echo $this->paymentAdapter->createAccount($name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment