Passed
Push — master ( 12c5ee...61343d )
by Herberto
01:56
created

ArrayConfig   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 47
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultDbName() 0 6 1
B getDataMapperFor() 0 29 2
1
<?php
2
3
namespace Hgraca\MicroOrm;
4
5
use Hgraca\Helper\ClassHelper;
6
use Hgraca\Helper\StringHelper;
7
use Hgraca\MicroOrm\DataMapper\DataMapper;
8
use Hgraca\MicroOrm\DataMapper\DataMapperInterface;
9
10
class ArrayConfig implements ConfigInterface
11
{
12
    /** @var array */
13
    protected $config;
14
15
    public function __construct(array $config)
16
    {
17
        $this->config = $config;
18
    }
19
20
    public function getDefaultDbName(): string
21
    {
22
        reset($this->config);
23
24
        return key($this->config);
25
    }
26
27
    public function getDataMapperFor(string $entityFqcn, string $dbName = ''): DataMapperInterface
28
    {
29
        $dbName = $dbName ? $dbName : $this->getDefaultDbName();
30
31
        $dataMapperArray = $this->config[$dbName]['entityDataMapper'][$entityFqcn] ?? [];
32
33
        $dataMapperArray['repositoryFqcn'] = $dataMapperArray['repositoryFqcn']
34
            ?? $this->config[$dbName]['repositoryFqcn']
35
            ?? StringHelper::replace(
36
                'Domain',
37
                'Persistence',
38
                StringHelper::replace('Entity', 'Repository', $entityFqcn)
39
            );
40
41
        $dataMapperArray['collectionFqcn'] = $dataMapperArray['collectionFqcn']
42
            ?? $this->config[$dbName]['collectionFqcn']
43
            ?? StringHelper::replace('Entity', 'Collection', $entityFqcn);
44
45
        $dataMapperArray['dateTimeFormat'] = $dataMapperArray['dateTimeFormat']
46
            ?? $this->config[$dbName]['dateTimeFormat']
47
            ?? 'Y-m-d H:i:s';
48
49
        $dataMapperArray['tableName'] = $dataMapperArray['tableName']
50
            ?? ClassHelper::extractCanonicalClassName($entityFqcn);
51
52
        $dataMapperArray['attributes'] = $dataMapperArray['attributes'] ?? [];
53
54
        return new DataMapper($entityFqcn, $dataMapperArray);
55
    }
56
}
57