TMapper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 96
ccs 22
cts 22
cp 1
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapper() 0 6 2
A setMapper() 0 3 1
A mapperFactory() 0 3 1
A count() 0 3 1
A load() 0 3 1
A save() 0 3 1
A loadMultiple() 0 3 1
A delete() 0 3 1
A mapperFromFactory() 0 7 2
1
<?php
2
3
namespace kalanis\kw_mapper\Records;
4
5
6
use kalanis\kw_mapper\MapperException;
7
use kalanis\kw_mapper\Mappers;
8
9
10
/**
11
 * trait TMapper
12
 * @package kalanis\kw_mapper\Records
13
 * Class to map entries to their respective values
14
 * The level of "obstruction" to accessing properties is necessary
15
 * or it could not be possible to guarantee content values.
16
 * The children must stay too simple to avoid some usual problems which came with multilevel extending
17
 */
18
trait TMapper
19
{
20
    protected ?Mappers\AMapper $mapper = null;
21
    private ?Mappers\Factory $mapperFactory = null;
22
23
    /**
24
     * @param string $name
25
     * @throws MapperException
26
     */
27 170
    final protected function setMapper(string $name): void
28
    {
29 170
        $this->mapper = $this->mapperFromFactory($name);
30
    }
31
32
    /**
33
     * @param string $name
34
     * @throws MapperException
35
     * @return Mappers\AMapper
36
     */
37 170
    protected function mapperFromFactory(string $name): Mappers\AMapper
38
    {
39
        // factory returns class as static instance, so it is not necessary to fill more memory with necessities
40 170
        if (empty($this->mapperFactory)) {
41 170
            $this->mapperFactory = $this->mapperFactory();
42
        }
43 170
        return $this->mapperFactory->getInstance($name);
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return $this->mapperFactory->/** @scrutinizer ignore-call */ getInstance($name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
    }
45
46
    /**
47
     * You can set own factory to load other mappers
48
     * @return Mappers\Factory
49
     */
50 170
    protected function mapperFactory(): Mappers\Factory
51
    {
52 170
        return new Mappers\Factory();
53
    }
54
55
    /**
56
     * @param bool $forceInsert
57
     * @throws MapperException
58
     * @return bool
59
     */
60 16
    final public function save(bool $forceInsert = false): bool
61
    {
62 16
        return $this->getMapper()->save($this->getSelf(), $forceInsert);
63
    }
64
65
    /**
66
     * @throws MapperException
67
     * @return bool
68
     */
69 17
    final public function load(): bool
70
    {
71 17
        return $this->getMapper()->load($this->getSelf());
72
    }
73
74
    /**
75
     * @throws MapperException
76
     * @return bool
77
     */
78 9
    final public function delete(): bool
79
    {
80 9
        return $this->getMapper()->delete($this->getSelf());
81
    }
82
83
    /**
84
     * @throws MapperException
85
     * @return int
86
     */
87 6
    final public function count(): int
88
    {
89 6
        return $this->getMapper()->countRecord($this->getSelf());
90
    }
91
92
    /**
93
     * @throws MapperException
94
     * @return ARecord[]
95
     */
96 24
    final public function loadMultiple(): array
97
    {
98 24
        return $this->getMapper()->loadMultiple($this->getSelf());
99
    }
100
101
    /**
102
     * @throws MapperException
103
     * @return Mappers\AMapper
104
     */
105 148
    public function getMapper(): Mappers\AMapper
106
    {
107 148
        if (empty($this->mapper)) {
108 1
            throw new MapperException('Unknown entry mapper');
109
        }
110 147
        return $this->mapper;
111
    }
112
113
    abstract protected function getSelf(): ARecord;
114
}
115