Test Failed
Push — master ( 6508c2...e5864c )
by Terzi
14:31
created

HandlesRelation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 4
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A relation() 0 3 1
A getForeignKey() 0 11 3
A relationModule() 0 4 1
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
        throw new Exception("Unable to resolve foreign key.");
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Field\Traits\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
45
    }
46
}
47