MapperFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 13
dl 0
loc 43
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 3
A __construct() 0 4 1
1
<?php
2
3
namespace Palladium\Component;
4
5
use RuntimeException;
6
use PDO;
7
use Palladium\Contract\CanCreateMapper;
8
9
class MapperFactory implements CanCreateMapper
10
{
11
12
    private $connection;
13
    private $cache = [];
14
    private $table;
15
16
    /**
17
     * Creates new factory instance
18
     *
19
     * @param PDO $connection
20
     * @param string $table A list of table name aliases
21
     */
22 27
    public function __construct(PDO $connection, string $table)
23
    {
24 27
        $this->connection = $connection;
25 27
        $this->table = $table;
26 27
    }
27
28
29
    /**
30
     * Method for retrieving an SQL data mapper instance
31
     *
32
     * @param string $className Fully qualified class name of the mapper
33
     *
34
     * @throws RuntimeException if mapper's class can't be found
35
     *
36
     * @return DataMapper
37
     */
38 27
    public function create(string $className)
39
    {
40 27
        if (array_key_exists($className, $this->cache)) {
41 11
            return $this->cache[$className];
42
        }
43
44 27
        if (!class_exists($className)) {
45 1
            throw new RuntimeException("Mapper not found. Attempted to load '{$className}'.");
46
        }
47
48 26
        $instance = new $className($this->connection, $this->table);
49 26
        $this->cache[$className] = $instance;
50
51 26
        return $instance;
52
    }
53
54
}
55