Completed
Pull Request — master (#274)
by
unknown
63:55 queued 33:13
created

FlatReference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 39
    public function __construct(string $className, $id)
17
    {
18 39
        $this->className = $className;
19 39
        $this->id = $id;
20 39
    }
21
22
    /**
23
     * Recreate the model instance that is referred to by this collection id
24
     * @return Model
25
     */
26 25
    public function instance(): Model
27
    {
28 25
        return (new $this->className)->withoutGlobalScopes()->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 34
    public function get(): string
42
    {
43 34
        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 1
    public function is(string $flatReferenceString): bool
52
    {
53 1
        return $this->get() === $flatReferenceString;
54
    }
55
56
    public function __toString()
57
    {
58
        return $this->get();
59
    }
60
}
61