RelationFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasOne() 0 3 1
A belongsTo() 0 3 1
A shareOne() 0 9 1
A __construct() 0 2 1
A shareMany() 0 9 1
A hasMany() 0 3 1
1
<?php
2
3
/**
4
 * Platine ORM
5
 *
6
 * Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine ORM
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file RelationFactory.php
33
 *
34
 *  The RelationFactory class
35
 *
36
 *  @package    Platine\Orm\Relation
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Orm\Relation;
48
49
use Closure;
50
51
/**
52
 * @class RelationFactory
53
 * @package Platine\Orm\Relation
54
 * @template TEntity as \Platine\Orm\Entity
55
 */
56
class RelationFactory
57
{
58
    /**
59
     * Create new instance
60
     * @param string $name
61
     * @param Closure $callback
62
     */
63
    public function __construct(protected string $name, protected Closure $callback)
64
    {
65
    }
66
67
    /**
68
     * Set has one relation
69
     * @param class-string<TEntity> $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TEntity> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TEntity>.
Loading history...
70
     * @param ForeignKey|null $foreignKey
71
     * @return Relation<TEntity>
72
     */
73
    public function hasOne(string $entityClass, ?ForeignKey $foreignKey = null): Relation
74
    {
75
        return ($this->callback)($this->name, new HasOne($entityClass, $foreignKey));
76
    }
77
78
    /**
79
     * Set has many relation
80
     * @param class-string<TEntity> $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TEntity> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TEntity>.
Loading history...
81
     * @param ForeignKey|null $foreignKey
82
     * @return Relation<TEntity>
83
     */
84
    public function hasMany(string $entityClass, ?ForeignKey $foreignKey = null): Relation
85
    {
86
        return ($this->callback)($this->name, new HasMany($entityClass, $foreignKey));
87
    }
88
89
    /**
90
     * Set belongs to relation
91
     * @param class-string<TEntity> $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TEntity> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TEntity>.
Loading history...
92
     * @param ForeignKey|null $foreignKey
93
     * @return Relation<TEntity>
94
     */
95
    public function belongsTo(string $entityClass, ?ForeignKey $foreignKey = null): Relation
96
    {
97
        return ($this->callback)($this->name, new BelongsTo($entityClass, $foreignKey));
98
    }
99
100
    /**
101
     * Set share one relation
102
     * @param class-string<TEntity> $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TEntity> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TEntity>.
Loading history...
103
     * @param ForeignKey|null $foreignKey
104
     * @param Junction|null $junction
105
     * @return Relation<TEntity>
106
     */
107
    public function shareOne(
108
        string $entityClass,
109
        ?ForeignKey $foreignKey = null,
110
        ?Junction $junction = null
111
    ): Relation {
112
        return ($this->callback)($this->name, new ShareOne(
113
            $entityClass,
114
            $foreignKey,
115
            $junction
116
        ));
117
    }
118
119
    /**
120
     * Set share many relation
121
     * @param class-string<TEntity> $entityClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TEntity> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TEntity>.
Loading history...
122
     * @param ForeignKey|null $foreignKey
123
     * @param Junction|null $junction
124
     * @return Relation<TEntity>
125
     */
126
    public function shareMany(
127
        string $entityClass,
128
        ?ForeignKey $foreignKey = null,
129
        ?Junction $junction = null
130
    ): Relation {
131
        return ($this->callback)($this->name, new ShareMany(
132
            $entityClass,
133
            $foreignKey,
134
            $junction
135
        ));
136
    }
137
}
138