Completed
Push — master ( a7a349...3fc402 )
by Guillermo A.
03:23
created

OperationFactory::getMarshaler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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