-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathAccount.php
More file actions
34 lines (29 loc) · 865 Bytes
/
Account.php
File metadata and controls
34 lines (29 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility;
abstract class Account
{
protected $balance;
/** @var Account */
protected $successor;
public function setNext(Account $account)
{
$this->successor = $account;
}
public function pay($amountToPay)
{
if ($this->canPay($amountToPay)) {
echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay,
get_called_class());
} elseif ($this->successor) {
echo sprintf('Cannot pay using %s. Proceeding ...' . PHP_EOL,
get_called_class());
$this->successor->pay($amountToPay);
} else {
throw new \Exception('None of the accounts have enough balance');
}
}
public function canPay($amount)
{
return $this->balance >= $amount;
}
}