Test Failed
Pull Request — master (#13)
by Rail
02:13
created

DynamicRelations::getDynamicRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
6
trait DynamicRelations
7
{
8
    use BaseEloquentOverrides;
9
10
    protected static $methodAllowed = [
11
        'getDynamicRelations',
12
        'hasDynamicRelation',
13
        'removeRelation',
14
        'defineRelation',
15
        'morphed_by_many',
16
        'has_many',
17
        'has_one',
18
        'belongs_to',
19
        'belongs_to_many',
20
        'morph_to_many',
21
        'morph_many',
22
        'morph_one',
23
        'morph_to',
24
        'has_many_through'
25
    ];
26
27
    /**
28
     * @var RelationStore
29
     */
30
    protected static $relationStore;
31
32
    /**
33
     * retrieve a new instance of the store
34
     *
35
     * @return RelationStore
36
     */
37 6
    public function newRelationStoreClass(): RelationStore
38
    {
39 6
        return new RelationStore();
40
    }
41
42
    /**
43
     * Dynamically handle calls to the class.
44
     *
45
     * @param  string  $method
46
     * @param  array   $parameters
47
     * @return mixed
48
     *
49
     * @throws \BadMethodCallException
50
     */
51 9
    public function __call($method, $parameters)
52
    {
53 9
        if (empty(static::$relationStore)) {
54 6
            static::$relationStore = $this->newRelationStoreClass();
55
        }
56
57 9
        $manager = new RelationManager(static::$relationStore);
58
59
        // Handle internal calls
60 9
        if (in_array($method, static::$methodAllowed)) {
61 9
            return $manager->$method(...array_merge([$this], $parameters));
62
        }
63
64
        // Handle relationships
65 7
        $dynamicRelation = static::$relationStore->get($this, $method);
66
67 7
        if ($dynamicRelation) {
68 6
            return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters);
69
        }
70
        
71 7
        return parent::__call($method, $parameters);
72
    }
73
74
    /**
75
     * Convert static call to 
76
     *
77
     * @param  string  $method
78
     * @param  array   $parameters
79
     * @return mixed
80
     *
81
     * @throws \BadMethodCallException
82
     */
83 9
    public static function __callStatic($method, $parameters)
84
    {
85 9
        if (in_array($method, static::$methodAllowed)) {
86 9
            $entity = new static;
87 9
            return $entity->$method(...$parameters);
88
        } else {
89 7
            return parent::__callStatic($method, $parameters);
90
        }
91
    }
92
  
93
94 1
    public static function forceEagerLoading(...$relation)
95
    {
96
        static::registerModelEvent('booting', function ($model) use ($relation) {
97
            $model->with = $model->with + $relation;
98 1
        });
99 1
    }
100
}
101