ExceptionFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 11
dl 0
loc 23
c 1
b 0
f 0
rs 10
ccs 10
cts 10
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A factory() 0 13 2
1
<?php
2
3
namespace Guillermoandrae\DynamoDb\Factory;
4
5
use Aws\DynamoDb\Exception\DynamoDbException;
6
use Guillermoandrae\DynamoDb\Exception\Exception;
7
use ReflectionClass;
8
use ReflectionException;
9
10
/**
11
 * Exception factory.
12
 *
13
 * @author Guillermo A. Fisher <[email protected]>
14
 */
15
final class ExceptionFactory
16
{
17
    /**
18
     * Returns an exception object.
19
     *
20
     * I'd rather deal with "physical" exceptions, so I created this class so that I can get actual objects.
21
     *
22
     * @param DynamoDbException $ex A DynamoDB exception.
23
     * @return Exception The exception object.
24
     */
25 12
    public static function factory(DynamoDbException $ex): Exception
26
    {
27
        try {
28 12
            $className = sprintf(
29 12
                '%s\%s',
30 12
                '\Guillermoandrae\DynamoDb\Exception',
31 12
                $ex->getAwsErrorCode()
32
            );
33 12
            $reflectionClass = new ReflectionClass($className);
34 11
            $args = [sprintf('An error has occurred => %s', $ex->getAwsErrorMessage()), $ex->getCode()];
35 11
            return $reflectionClass->newInstanceArgs($args);
36 1
        } catch (ReflectionException $ex) {
37 1
            return new Exception($ex->getMessage(), $ex->getCode());
38
        }
39
    }
40
}
41