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

CustomStructure   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 92
ccs 23
cts 28
cp 0.8214
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTable() 0 12 3
A registerScopes() 0 10 3
A getActiveRowClass() 0 6 2
A getSelectionClass() 0 6 2
A getScopes() 0 6 2
A getScope() 0 6 2
1
<?php
2
3
namespace SimpleMapper\Structure;
4
5
use SimpleMapper\ActiveRow;
6
use SimpleMapper\Exception\SimpleMapperException;
7
use SimpleMapper\Scope\Scope;
8
use SimpleMapper\Selection;
9
10
class CustomStructure implements Structure
11
{
12
    /** @var array */
13
    protected $data = [];
14
15
    /**
16
     * @param string $table
17
     * @param string|null $activeRowClass
18
     * @param string|null $selectionClass
19
     * @return CustomStructure
20
     * @throws SimpleMapperException
21
     */
22 62
    public function registerTable(string $table, string $activeRowClass = null, string $selectionClass = null): CustomStructure
23
    {
24 62
        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...
25 62
            $this->data[$table]['row'] = $activeRowClass;
26
        }
27
28 62
        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...
29 62
            $this->data[$table]['selection'] = $selectionClass;
30
        }
31
32 62
        return $this;
33
    }
34
35
    /**
36
     * Register table scopes (for repository and selection)
37
     * @param string $table
38
     * @param array $scopes
39
     * @throws SimpleMapperException
40
     * @internal
41
     */
42 10
    public function registerScopes(string $table, array $scopes): void
43
    {
44 10
        foreach ($scopes as $scope) {
45 10
            if (!($scope instanceof Scope)) {
46
                throw new SimpleMapperException('Scopes can be only of class ' . Scope::class);
47
            }
48
49 10
            $this->data[$table]['scopes'][$scope->getName()] = $scope;
50
        }
51 10
    }
52
53
    /**
54
     * Fetch row class by table
55
     * @param string $table
56
     * @return string
57
     */
58 40
    public function getActiveRowClass(string $table): string
59
    {
60 40
        return isset($this->data[$table]['row'])
61 38
            ? $this->data[$table]['row']
62 40
            : ActiveRow::class;
63
    }
64
65
    /**
66
     * Fetch selection class by table
67
     * @param string $table
68
     * @return string
69
     */
70 16
    public function getSelectionClass(string $table): string
71
    {
72 16
        return isset($this->data[$table]['selection'])
73 10
            ? $this->data[$table]['selection']
74 16
            : Selection::class;
75
    }
76
77
    /**
78
     * Returns all scopes registered for table
79
     * @param string $table
80
     * @return array
81
     */
82
    public function getScopes(string $table): array
83
    {
84
        return isset($this->data[$table]['scopes'])
85
            ? $this->data[$table]['scopes']
86
            : [];
87
    }
88
89
    /**
90
     * Returns one scope
91
     * @param string $table
92
     * @param string $scope
93
     * @return Scope|null
94
     */
95 2
    public function getScope(string $table, string $scope): ?Scope
96
    {
97 2
        return isset($this->data[$table]['scopes'][$scope])
98 2
            ? $this->data[$table]['scopes'][$scope]
99 2
            : null;
100
    }
101
}
102