Completed
Push — master ( 6067eb...3f355c )
by Samuel
15:57 queued 01:01
created

Mapper::mapRepository()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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