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

FlatReference::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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