Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

Morphables::instance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Thinktomorrow\Chief\Concerns\Morphable;
4
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
7
class Morphables
8
{
9 150
    public static function instance(string $key, $attributes = [])
10
    {
11 150
        $class = $key;
12
13
        // We assume the key is the full namespaced class or a string altered by the relation morph map.
14 150
        if ($morphedModel = Relation::getMorphedModel($key)) {
15 80
            $class = $morphedModel;
16
        }
17
18 150
        if (!class_exists($class)) {
19 1
            throw new NotFoundMorphKey('No class found by morphkey [' . $class . ']. Make sure that the morphkey is a valid class reference.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
20
        }
21
22 149
        return new $class($attributes);
23
    }
24
}
25