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

HandlesRelation::relationModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
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