ExceptionFactory::factory()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.9332
cc 2
nc 5
nop 1
crap 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