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

HandlesRelation::getForeignKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 12
rs 10
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