Last active
February 15, 2023 14:05
-
-
Save diloabininyeri/744b37037c09eb2b2ca8aea6b87be9d6 to your computer and use it in GitHub Desktop.
the pick one a bank in the too complex conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User | |
{ | |
} | |
interface BankWithdraw | |
{ | |
public function withdraw(User $user): array; | |
} | |
abstract class DecideBank | |
{ | |
protected ?self $bank; | |
protected ?self $defaultBank; | |
public function default(self $defaultBank): self | |
{ | |
$this->defaultBank = $defaultBank; | |
return $this; | |
} | |
public function append(self $bank): self | |
{ | |
return $this->bank = $bank; | |
} | |
protected function noDecide(): ?self | |
{ | |
return $this->bank?->logic(); | |
} | |
abstract public function logic(): mixed; | |
abstract public function getBankAccount(): BankWithdraw; | |
} | |
class GuessBank extends DecideBank | |
{ | |
public function logic(): mixed | |
{ | |
return $this->bank?->logic() ?: $this->defaultBank; | |
} | |
public function getBankAccount(): BankWithdraw | |
{ | |
return new class implements BankWithdraw { | |
public function withdraw(User $user): array | |
{ | |
return [ | |
'class'=>__CLASS__, | |
]; | |
} | |
}; | |
} | |
} | |
class Akbank extends DecideBank | |
{ | |
public function logic(): mixed | |
{ | |
return $this->noDecide(); | |
} | |
public function getBankAccount(): BankWithdraw | |
{ | |
return new class implements BankWithdraw { | |
public function withdraw(User $user): array | |
{ | |
return [ | |
'class'=>__CLASS__, | |
]; | |
} | |
}; | |
} | |
} | |
class Garanti extends DecideBank | |
{ | |
public function logic(): mixed | |
{ | |
return $this->noDecide(); | |
} | |
public function getBankAccount(): BankWithdraw | |
{ | |
return new class implements BankWithdraw { | |
public function withdraw(User $user): array | |
{ | |
return [ | |
'class'=>__CLASS__, | |
]; | |
} | |
}; | |
} | |
} | |
class Masaka extends DecideBank | |
{ | |
public function logic(): mixed | |
{ | |
return $this; | |
} | |
public function getBankAccount(): BankWithdraw | |
{ | |
return new class implements BankWithdraw { | |
public function withdraw(User $user): array | |
{ | |
return [ | |
'class' => __CLASS__ | |
]; | |
} | |
}; | |
} | |
} | |
class YapiKredi extends DecideBank | |
{ | |
public function logic(): mixed | |
{ | |
return $this->noDecide(); | |
} | |
public function getBankAccount(): BankWithdraw | |
{ | |
return new class implements BankWithdraw { | |
public function withdraw(User $user): array | |
{ | |
return [ | |
'class'=>__CLASS__, | |
]; | |
} | |
}; | |
} | |
} | |
function get_bank(): BankWithdraw | |
{ | |
$guessBank = new GuessBank(); | |
$guessBank | |
->default(new Akbank()) | |
->append(new Garanti()) | |
->append(new Masaka()) | |
->append(new YapiKredi()); | |
return $guessBank->logic()->getBankAccount(); | |
} | |
$withDrawResponse=get_bank()->withdraw(new User()); | |
print_r($withDrawResponse); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment