Repository navigation
Expand file tree
/
Copy pathOfflineConfig.php
More file actions
72 lines (61 loc) · 2.54 KB
/
Copy pathOfflineConfig.php
File metadata and controls
72 lines (61 loc) · 2.54 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace Payum\Offline;
use Payum\Core\Config\GatewayConfig;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Handler\HandlerInterface;
use Payum\Core\Handler\HandlerMap;
use function array_keys;
use function implode;
use function is_string;
use function sprintf;
class OfflineConfig implements GatewayConfig
{
/**
* @var array<class-string<HandlerInterface>, HandlerInterface|class-string<HandlerInterface>>
*/
public readonly array $handlers;
/**
* @param bool $paid whether a payment counts as paid as soon as it is captured or authorized. A payment's
* own `paid` detail, when set, wins
* @param list<HandlerInterface|class-string<HandlerInterface>> $handlers each replaces the gateway's handler
* for the command it handles. A class
* name is autowired
*/
public function __construct(
public readonly bool $paid = true,
array $handlers = [],
) {
$gatewayClass = $this->getGatewayClass();
$shipped = HandlerMap::fromHandlers((new $gatewayClass())->handlers())->bindings();
$replacements = [];
foreach ($handlers as $handler) {
$class = is_string($handler) ? $handler : $handler::class;
foreach (array_keys(HandlerMap::fromHandlers([$class])->bindings()) as $interface) {
if (! isset($shipped[$interface])) {
throw new InvalidArgumentException(sprintf(
'%s implements %s, but %s only takes the commands of %s.',
$class,
$interface,
$gatewayClass,
implode(', ', array_keys($shipped)),
));
}
if (isset($replacements[$interface])) {
throw new InvalidArgumentException(sprintf(
'Both %s and %s implement %s. Give one handler per command.',
is_string($replacements[$interface]) ? $replacements[$interface] : $replacements[$interface]::class,
$class,
$interface,
));
}
$replacements[$interface] = $handler;
}
}
$this->handlers = $replacements;
}
public function getGatewayClass(): string
{
return OfflineGateway::class;
}
}