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
declare(strict_types=1);
3
4
namespace SimpleMapper;
5
6
use SimpleMapper\Structure\Structure;
7
8
class Mapper
9
{
10
    /** @var Structure */
11
    private $structure;
12
13
    /** @var array */
14
    private $repositories = [];
15
16
    /**
17
     * @param Structure $structure
18
     */
19 68
    public function __construct(Structure $structure)
20
    {
21 68
        $this->structure = $structure;
22 68
    }
23
24
    /**
25
     * Map classes and scopes by repository
26
     * @param Repository $repository
27
     * @param string|null $activeRowClass
28
     * @param string|null $selectionClass
29
     * @return Mapper
30
     */
31 68
    public function mapRepository(Repository $repository, string $activeRowClass = null, string $selectionClass = null): Mapper
32
    {
33 68
        $this->repositories[get_class($repository)] = $repository;
34 68
        $repository->setStructure($this->structure);
35
36 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...
37 68
            $this->structure->registerActiveRowClass($repository::getTableName(), $activeRowClass);
38
        }
39
40 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...
41 68
            $this->structure->registerSelectionClass($repository::getTableName(), $selectionClass);
42
        }
43
44 68
        return $this;
45
    }
46
47
    /**
48
     * Map classes only by table name
49
     * @param string $tableName
50
     * @param string|null $activeRowClass
51
     * @param string|null $selectionClass
52
     * @return Mapper
53
     */
54 68
    public function mapTableName(string $tableName, string $activeRowClass = null, string $selectionClass = null): Mapper
55
    {
56 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...
57 68
            $this->structure->registerActiveRowClass($tableName, $activeRowClass);
58
        }
59
60 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...
61 68
            $this->structure->registerSelectionClass($tableName, $selectionClass);
62
        }
63
64 68
        return $this;
65
    }
66
67
    /**
68
     * @param string $class
69
     * @return null|Repository
70
     */
71 68
    public function getRepository(string $class): ?Repository
72
    {
73 68
        return $this->repositories[$class] ?? null;
74
    }
75
}
76