Passed
Push — master ( f5a05a...0e54de )
by Aleksandar
02:23
created

RelationalMapper::insertMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ArekX\PQL\Orm;
4
5
use ArekX\PQL\Contracts\Driver;
6
use ArekX\PQL\Contracts\QueryBuilder;
7
8
/**
9
 * Relational Mapper for mapping
10
 * models to tables.
11
 *
12
 */
13
class RelationalMapper
14
{
15
    /**
16
     * Current driver in use.
17
     * @var Driver
18
     */
19
    public Driver $driver;
20
21
    /**
22
     * Current query builder in use.
23
     *
24
     * @var QueryBuilder
25
     */
26
    public QueryBuilder $builder;
27
28
    /**
29
     * Compiler for model structure
30
     *
31
     * @var ModelCompiler
32
     */
33
    protected ModelCompiler $compiler;
34
35
    /**
36
     * Creates a new instance of this mapper
37
     *
38
     * @param Driver|null $driver Driver to be set to be used
39
     * @param QueryBuilder|null $builder Builder to be used
40
     * @return static
41
     */
42 1
    public static function create(?Driver $driver = null, ?QueryBuilder $builder = null): static
43
    {
44 1
        return new static($driver, $builder);
45
    }
46
47
    /**
48
     * Creates new instance of this mapper
49
     *
50
     * @param Driver|null $driver Driver to be set to be used
51
     * @param QueryBuilder|null $builder Builder to be used
52
     */
53 1
    public function __construct(?Driver $driver = null, ?QueryBuilder $builder = null)
54
    {
55 1
        if ($driver) {
56 1
            $this->useDriver($driver);
57
        }
58
59 1
        if ($builder) {
60 1
            $this->useBuilder($builder);
61
        }
62
63 1
        $this->compiler = new ModelCompiler();
64
    }
65
66
    /**
67
     * Set a driver to be used for queries.
68
     *
69
     * @param Driver $driver Driver to be used.
70
     * @return $this
71
     */
72 1
    public function useDriver(Driver $driver): static
73
    {
74 1
        $this->driver = $driver;
75 1
        return $this;
76
    }
77
78
    /**
79
     * Set query builder to be used.
80
     *
81
     * @param QueryBuilder $builder Builder to be used.
82
     * @return $this
83
     */
84 1
    public function useBuilder(QueryBuilder $builder): static
85
    {
86 1
        $this->builder = $builder;
87 1
        return $this;
88
    }
89
90
91 1
    public function toModelStructure(string $modelClass): array
92
    {
93
        // Relational handler for complex models
94
        // Relational handler should have find, save, delete implemented for the model class
95
        // and the model class should not have any attributes.
96
        // relational handler needs to be registered in this mapper to be used
97
        // models are missing multi primary keys relations
98
        // columns are missing a primary key flag (for insert/update check)
99 1
        return $this->compiler->resolve($modelClass);
100
    }
101
102
    public function find(string $modelClass)
0 ignored issues
show
Unused Code introduced by
The parameter $modelClass is not used and could be removed. ( Ignorable by Annotation )

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

102
    public function find(/** @scrutinizer ignore-unused */ string $modelClass)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
105
    }
106
107
    public function save($model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

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

107
    public function save(/** @scrutinizer ignore-unused */ $model)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
110
    }
111
112
    public function delete($model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

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

112
    public function delete(/** @scrutinizer ignore-unused */ $model)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
115
    }
116
}