HandlesRelation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 41
ccs 0
cts 4
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A relation() 0 3 1
A relationModule() 0 4 1
A getForeignKey() 0 15 4
1
<?php
2
3
namespace Terranet\Administrator\Field\Traits;
4
5
use Terranet\Administrator\Architect;
6
use Terranet\Administrator\Contracts\Module;
7
8
trait HandlesRelation
9
{
10
    /**
11
     * @return mixed
12
     */
13
    public function relation()
14
    {
15
        return \call_user_func([$this->model, $this->id]);
16
    }
17
18
    /**
19
     * Finds a module this relation belongs to.
20
     *
21
     * @return null|
22
     */
23
    public function relationModule(): ?Module
24
    {
25
        return Architect::resourceByEntity(
26
            $this->relation()->getRelated()
27
        );
28
    }
29
30
    /**
31
     * @param $relation
32
     * @return mixed
33
     */
34
    public function getForeignKey($relation)
35
    {
36
        if (method_exists($relation, 'getForeignKey')) {
37
            return $relation->getForeignKey();
38
        }
39
40
        if (method_exists($relation, 'getForeignKeyName')) {
41
            return $relation->getForeignKeyName();
42
        }
43
44
        if (method_exists($relation, 'getForeignPivotKeyName')) {
45
            return $relation->getForeignPivotKeyName();
46
        }
47
48
        throw new \Exception("Unable to resolve foreign key.");
49
    }
50
}
51