Passed
Push — master ( 0db7f5...3c2912 )
by Ben
74:34 queued 68:06
created

FlatReference::is()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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 29
    public function __construct(string $className, $id)
17
    {
18 29
        $this->className = $className;
19 29
        $this->id = $id;
20 29
    }
21
22
    /**
23
     * Recreate the model instance that is referred to by this collection id
24
     * @return Model
25
     */
26 18
    public function instance(): Model
27
    {
28 18
        return (new $this->className)->findOrFail($this->id);
29
    }
30
31
    public function id()
32
    {
33
        return $this->id;
34
    }
35
36 25
    public function className(): string
37
    {
38 25
        return $this->className;
39
    }
40
41 1
    public function get(): string
42
    {
43 1
        return $this->className.'@'.$this->id;
44
    }
45
46
    public function equals($other): bool
47
    {
48
        return (get_class($this) === get_class($other) && $this->get() === $other->get());
49
    }
50
51
    public function is(string $flatReferenceString): bool
52
    {
53
        return $this->get() === $flatReferenceString;
54
    }
55
56
    public function __toString()
57
    {
58
        return $this->get();
59
    }
60
}
61