|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\DynamoDb\DynamoDbClient; |
|
6
|
|
|
use Aws\DynamoDb\Marshaler; |
|
7
|
|
|
use Guillermoandrae\DynamoDb\Contract\OperationInterface; |
|
8
|
|
|
use Guillermoandrae\DynamoDb\Exception\Exception; |
|
9
|
|
|
use Guillermoandrae\DynamoDb\Helper\Inflector; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use ReflectionException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Operation factory. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Guillermo A. Fisher <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class OperationFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DynamoDbClient The DynamoDb client. |
|
22
|
|
|
*/ |
|
23
|
|
|
private static DynamoDbClient $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Marshaler The Marshaler. |
|
27
|
|
|
*/ |
|
28
|
|
|
private static Marshaler $marshaler; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Creates and returns the desired request. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $type The request type. |
|
34
|
|
|
* @param mixed $options,... OPTIONAL The request options. |
|
35
|
|
|
* @return OperationInterface The request. |
|
36
|
|
|
* @throws Exception Thrown when an error occurs in creating the request. |
|
37
|
|
|
*/ |
|
38
|
37 |
|
public static function factory(string $type, ...$options): OperationInterface |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
37 |
|
$className = sprintf( |
|
42
|
37 |
|
'%s\%sOperation', |
|
43
|
37 |
|
'\Guillermoandrae\DynamoDb\Operation', |
|
44
|
37 |
|
Inflector::camelize($type) |
|
45
|
|
|
); |
|
46
|
37 |
|
$reflectionClass = new ReflectionClass($className); |
|
47
|
36 |
|
$args = [self::getClient(), self::getMarshaler()]; |
|
48
|
36 |
|
foreach ($options as $option) { |
|
49
|
23 |
|
$args[] = $option; |
|
50
|
|
|
} |
|
51
|
36 |
|
return $reflectionClass->newInstanceArgs($args); |
|
52
|
1 |
|
} catch (ReflectionException $ex) { |
|
53
|
1 |
|
throw new Exception( |
|
54
|
1 |
|
sprintf('The %s operation does not exist.', $type) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the DynamoDb client. |
|
61
|
|
|
* |
|
62
|
|
|
* @return DynamoDbClient The DynamoDb client. |
|
63
|
|
|
*/ |
|
64
|
36 |
|
public static function getClient(): DynamoDbClient |
|
65
|
|
|
{ |
|
66
|
36 |
|
return self::$client; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Registers the DynamoDb client with this class. |
|
71
|
|
|
* |
|
72
|
|
|
* @param DynamoDbClient $client The DynamoDb client. |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
36 |
|
public static function setClient(DynamoDbClient $client): void |
|
76
|
|
|
{ |
|
77
|
36 |
|
self::$client = $client; |
|
78
|
36 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the Marshaler. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Marshaler The Marshaler. |
|
84
|
|
|
*/ |
|
85
|
36 |
|
public static function getMarshaler(): Marshaler |
|
86
|
|
|
{ |
|
87
|
36 |
|
return self::$marshaler; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Registers the Marshaler with this class. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Marshaler $marshaler The Marshaler. |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
36 |
|
public static function setMarshaler(Marshaler $marshaler): void |
|
97
|
|
|
{ |
|
98
|
36 |
|
self::$marshaler = $marshaler; |
|
99
|
36 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|