Completed
Push — master ( 20a668...02e94f )
by Guillermo A.
02:17
created

RequestFactory::factory()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 9.7998
cc 3
nc 9
nop 2
crap 3
1
<?php
2
3
namespace Guillermoandrae\DynamoDb;
4
5
use Aws\DynamoDb\Marshaler;
6
use ICanBoogie\Inflector;
7
use ReflectionClass;
8
use ReflectionException;
9
10
final class RequestFactory
11
{
12
    /**
13
     * @var Marshaler The Marshaler.
14
     */
15
    private static $marshaler;
16
    
17
    /**
18
     * Creates and returns the desired request.
19
     *
20
     * @param string $type The request type.
21
     * @param mixed $options,... OPTIONAL The request options.
22
     * @return RequestInterface The request.
23
     * @throws Exception Thrown when an error occurs in creating the request.
24
     */
25 20
    public static function factory(string $type, ...$options): RequestInterface
26
    {
27
        try {
28 20
            $className = sprintf(
29 20
                '%s\%sRequest',
30 20
                __NAMESPACE__,
31 20
                Inflector::get()->camelize($type)
32
            );
33 20
            $reflectionClass = new ReflectionClass($className);
34 19
            $args = [self::getMarshaler()];
35 19
            foreach ($options as $option) {
36 18
                $args[] = $option;
37
            }
38 19
            $request = $reflectionClass->newInstanceArgs($args);
39 19
            return $request;
40 1
        } catch (ReflectionException $ex) {
41 1
            throw new Exception(
42 1
                sprintf('The %s request does not exist.', $type)
43
            );
44
        }
45
    }
46
47
    /**
48
     * Registers the Marshaler with this class.
49
     *
50
     * @param Marshaler $marshaler The Marshaler.
51
     * @return void
52
     */
53 19
    public static function setMarshaler(Marshaler $marshaler): void
54
    {
55 19
        self::$marshaler = $marshaler;
56 19
    }
57
58
    /**
59
     * Returns the Marshaler.
60
     *
61
     * @return Marshaler The Marshaler.
62
     */
63 19
    public static function getMarshaler(): Marshaler
64
    {
65 19
        return self::$marshaler;
66
    }
67
}
68