Last active
December 21, 2017 12:43
-
-
Save vlastv/4c5926ba4744a1f0761c53ecd1877ca2 to your computer and use it in GitHub Desktop.
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 BalanceTransaction | |
{ | |
public function __construct(User $user, $amount, $reason) | |
{ | |
$this->updateReason($reason); | |
} | |
protected function updateReason($reason) | |
{ | |
$this->reason = new TransactionReason($this, $reason); | |
} | |
} | |
class BalanceTransactionRepository extends ServiceEntityRepository | |
{ | |
public function __construct(ManagerRegistry $registry) | |
{ | |
parent::__construrct($registry, BalanceTransaction::class); | |
} | |
public function add(BalanceTransaction $transaction) | |
{ | |
$this->em->persist($transaction); | |
$this->em->flush(); | |
} | |
} | |
class AddTransaction interface AddTransactionInterface | |
{ | |
private $transactions; | |
private $dispatcher; | |
private $validator; | |
public function __construct(BalanceTransactionRepository $transactions, ValidatorInterface $validator, EventDispatcherInterface $dispatcher) | |
{ | |
$this->transactions = $transactions; | |
$this->logger = new NullLogger(); | |
$this->dispatcher = $dispatcher; | |
$this->validator = $validator; | |
} | |
public function setLogger(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function add(UserInterface $user, $amount, $reason) | |
{ | |
$transaction = new BalanceTransaction($user, $amount, $reason); | |
$errors = $this->validator->validate($transaction); | |
if (count($errors) > 0) { | |
throw new Exception(''); | |
} | |
$this->transactions->add($transaction); | |
$this->dispatcher->dispatch('transaction_added', new Event($transaction)); | |
} | |
} | |
class TransactionalAddTransaction implements AddTransactionInterface | |
{ | |
private $conneciton; | |
private $handler; | |
public function __construct(Connection $conneciton, AddTransaction $handler) | |
{ | |
$this->conneciton = $conneciton; | |
$this->handler = $handler; | |
} | |
public function add(UserInterface $user, $amount) | |
{ | |
$this->connection->beginTransaction(); | |
try { | |
$this->handler->add($user, $amount); | |
$this->connection->commit(); | |
} catch (\Exception $exception) { | |
$this->connection->rollback(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment