Completed
Pull Request — master (#9)
by Samuel
03:17
created

Mapper::mapRepository()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 4
nop 3
crap 3
1
<?php
2
3
namespace SimpleMapper;
4
5
use SimpleMapper\Structure\Structure;
6
7
class Mapper
8
{
9
    /** @var Structure */
10
    private $structure;
11
12
    /** @var array */
13
    private $repositories = [];
14
15
    /**
16
     * @param Structure $structure
17
     */
18 68
    public function __construct(Structure $structure)
19
    {
20 68
        $this->structure = $structure;
21 68
    }
22
23
    /**
24
     * Map classes and scopes by repository
25
     * @param Repository $repository
26
     * @param string|null $activeRowClass
27
     * @param string|null $selectionClass
28
     * @return Mapper
29
     */
30 68
    public function mapRepository(Repository $repository, string $activeRowClass = null, string $selectionClass = null): Mapper
31
    {
32 68
        $this->repositories[get_class($repository)] = $repository;
33 68
        $repository->setStructure($this->structure);
34
35 68
        if ($activeRowClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $activeRowClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
36 68
            $this->structure->registerActiveRowClass($repository::getTableName(), $activeRowClass);
37
        }
38
39 68
        if ($selectionClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectionClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
40 68
            $this->structure->registerSelectionClass($repository::getTableName(), $selectionClass);
41
        }
42
43 68
        return $this;
44
    }
45
46
    /**
47
     * Map classes only by table name
48
     * @param string $tableName
49
     * @param string|null $activeRowClass
50
     * @param string|null $selectionClass
51
     * @return Mapper
52
     */
53 68
    public function mapTableName(string $tableName, string $activeRowClass = null, string $selectionClass = null): Mapper
54
    {
55 68
        if ($activeRowClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $activeRowClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
56 68
            $this->structure->registerActiveRowClass($tableName, $activeRowClass);
57
        }
58
59 68
        if ($selectionClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectionClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60 68
            $this->structure->registerSelectionClass($tableName, $selectionClass);
61
        }
62
63 68
        return $this;
64
    }
65
66
    /**
67
     * @param string $class
68
     * @return null|Repository
69
     */
70 68
    public function getRepository(string $class): ?Repository
71
    {
72 68
        return $this->repositories[$class] ?? null;
73
    }
74
}
75