Completed
Push — master ( cd48f7...0c447e )
by Guillermo A.
02:29
created

ExceptionFactory::factory()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 13
ccs 8
cts 10
cp 0.8
rs 9.9332
c 1
b 0
f 0
cc 2
nc 5
nop 1
crap 2.032
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
final class ExceptionFactory
11
{
12 9
    public static function factory(DynamoDbException $ex): Exception
13
    {
14
        try {
15 9
            $className = sprintf(
16 9
                '%s\%s',
17 9
                '\Guillermoandrae\DynamoDb\Exception',
18 9
                $ex->getAwsErrorCode()
19
            );
20 9
            $reflectionClass = new ReflectionClass($className);
21 9
            $args = [sprintf('An error has occurred => %s', $ex->getAwsErrorMessage()), $ex->getCode()];
22 9
            return $reflectionClass->newInstanceArgs($args);
23
        } catch (ReflectionException $ex) {
24
            return new Exception($ex->getMessage(), $ex->getCode());
25
        }
26
    }
27
}
28