OperationFactory.php
1.49 KB
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
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace App\Utils;
use App\Enum\CurrencyEnum;
use App\Enum\OperationEnum;
use App\Flux\AccountableInterface;
use App\Flux\FluxInterface;
use App\Flux\OperationInterface;
/**
* Operation factory.
*/
class OperationFactory
{
public static function getOperation(FluxInterface $flux, AccountableInterface $object, string $currency, float $montant): OperationInterface
{
$ref = new \ReflectionClass($object);
$type = 'OPERATION_' . strtoupper($ref->getShortName());
if (!in_array($type, OperationEnum::getAvailableTypes())) {
throw new \Exception("Opération impossible ! Type d'operation " . $type . ' inexistant');
// @TODO
// throw new IllegalTypeException($type);
}
if (!in_array($currency, CurrencyEnum::getAvailableTypes())) {
throw new \Exception('Opération impossible ! Type de currency ' . $currency . ' inexistant');
// @TODO
// throw new IllegalCurrencyException($currency);
}
$class = OperationEnum::getClassName($type);
$operation = new $class();
$account = $object->getAccountWithCurrency($currency);
if (null == $account) {
throw new \Exception("No account for object $object #{$object->getId()}! and currency : $currency");
}
$operation
->setMontant($montant)
->setCurrency($currency)
->setFlux($flux)
->setAccount($account);
return $operation;
}
}