Passed
Push — ft/fields-refactor ( 34e13a...220f3b )
by Ben
81:47
created

FlatReference   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 8
eloc 11
dl 0
loc 45
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 3 1
A __construct() 0 4 1
A equals() 0 3 2
A className() 0 3 1
A id() 0 3 1
A __toString() 0 3 1
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\FlatReferences;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
class FlatReference
10
{
11
    /** @var string */
12
    protected $className;
13
14
    protected $id;
15
16 37
    public function __construct(string $className, $id)
17
    {
18 37
        $this->className = $className;
19 37
        $this->id = $id;
20 37
    }
21
22
    /**
23
     * Recreate the model instance that is referred to by this collection id
24
     * @return Model
25
     */
26 23
    public function instance(): Model
27
    {
28 23
        return (new $this->className)->findOrFail($this->id);
29
    }
30
31 3
    public function id()
32
    {
33 3
        return $this->id;
34
    }
35
36 3
    public function className(): string
37
    {
38 3
        return $this->className;
39
    }
40
41 33
    public function get(): string
42
    {
43 33
        return $this->className.'@'.$this->id;
44
    }
45
46 1
    public function equals($other): bool
47
    {
48 1
        return (get_class($this) === get_class($other) && $this->get() === $other->get());
49
    }
50
51
    public function __toString()
52
    {
53
        return $this->get();
54
    }
55
}
56