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

DynamicRelations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 88
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

4 Methods

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