Completed
Push — master ( 1ec9e1...9268d3 )
by Jared
01:31
created

RelationFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 83
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B make() 0 31 6
A hasOne() 0 4 1
A belongsTo() 0 4 1
A hasMany() 0 4 1
A belongsToMany() 0 4 1
1
<?php
2
3
namespace Pulsar\Relation;
4
5
use InvalidArgumentException;
6
use Pulsar\Model;
7
use Pulsar\Property;
8
9
class RelationFactory
10
{
11
    public static function make(Model $model, string $propertyName, Property $property): Relation
12
    {
13
        $relationModelClass = $property->getRelation();
14
        if (!$relationModelClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationModelClass of type string|null is loosely compared to false; 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...
15
            throw new InvalidArgumentException('Property "'.$propertyName.'" does not have a relationship.');
16
        }
17
18
        $foreignKey = $property->getForeignKey();
19
        $localKey = $property->getLocalKey();
20
        $relationType = $property->getRelationType();
21
22
        if (Model::RELATIONSHIP_HAS_ONE == $relationType) {
23
            return self::hasOne($model, $relationModelClass, $foreignKey, $localKey);
24
        }
25
26
        if (Model::RELATIONSHIP_HAS_MANY == $relationType) {
27
            return self::hasMany($model, $relationModelClass, $foreignKey, $localKey);
28
        }
29
30
        if (Model::RELATIONSHIP_BELONGS_TO == $relationType) {
31
            return self::belongsTo($model, $relationModelClass, $foreignKey, $localKey);
32
        }
33
34
        if (Model::RELATIONSHIP_BELONGS_TO_MANY == $relationType) {
35
            $pivotTable = $property->getPivotTablename();
36
37
            return self::belongsToMany($model, $relationModelClass, $pivotTable, $foreignKey, $localKey);
38
        }
39
40
        throw new InvalidArgumentException('Relationship type on "'.$propertyName.'" property not supported: '.$relationType);
41
    }
42
43
    /**
44
     * Creates the parent side of a One-To-One relationship.
45
     *
46
     * @param string $model      foreign model class
47
     * @param string $foreignKey identifying key on foreign model
48
     * @param string $localKey   identifying key on local model
49
     */
50
    public static function hasOne(Model $theModel, $model, $foreignKey = '', $localKey = ''): HasOne
51
    {
52
        return new HasOne($theModel, $localKey, $model, $foreignKey);
53
    }
54
55
    /**
56
     * Creates the child side of a One-To-One or One-To-Many relationship.
57
     *
58
     * @param string $model      foreign model class
59
     * @param string $foreignKey identifying key on foreign model
60
     * @param string $localKey   identifying key on local model
61
     */
62
    public static function belongsTo(Model $theModel, $model, $foreignKey = '', $localKey = ''): BelongsTo
63
    {
64
        return new BelongsTo($theModel, $localKey, $model, $foreignKey);
65
    }
66
67
    /**
68
     * Creates the parent side of a Many-To-One or Many-To-Many relationship.
69
     *
70
     * @param string $model      foreign model class
71
     * @param string $foreignKey identifying key on foreign model
72
     * @param string $localKey   identifying key on local model
73
     */
74
    public static function hasMany(Model $theModel, $model, $foreignKey = '', $localKey = ''): HasMany
75
    {
76
        return new HasMany($theModel, $localKey, $model, $foreignKey);
77
    }
78
79
    /**
80
     * Creates the child side of a Many-To-Many relationship.
81
     *
82
     * @param string $model      foreign model class
83
     * @param string $tablename  pivot table name
84
     * @param string $foreignKey identifying key on foreign model
85
     * @param string $localKey   identifying key on local model
86
     */
87
    public static function belongsToMany(Model $theModel, $model, $tablename = '', $foreignKey = '', $localKey = ''): BelongsToMany
88
    {
89
        return new BelongsToMany($theModel, $localKey, $tablename, $model, $foreignKey);
90
    }
91
}
92