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

StoredSetReference::flatReferenceLabel()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Sets;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Thinktomorrow\Chief\FlatReferences\FlatReference;
8
use Thinktomorrow\Chief\Relations\ActingAsChild;
9
use Thinktomorrow\Chief\Relations\ActsAsChild;
10
11
/**
12
 * @property $id
13
 * @property $action
14
 * @property $key
15
 * @property $parameters
16
 */
17
class StoredSetReference extends Model implements ActsAsChild
18
{
19
    use ActingAsChild;
20
21
    public $table = 'pagesets'; // TODO: this should change to 'sets' to represent its generic nature.
22
    public $guarded = [];
23
    public $timestamps = false;
24
    public $casts = [
25
        'parameters' => 'array',
26
    ];
27
28
    /**
29
     * Run the query and collect the resulting pages into a Set object.
30
     */
31 3
    public function toSet()
32
    {
33 3
        return Set::fromReference($this->toReference());
34
    }
35
36 5
    public function toReference(): SetReference
37
    {
38
        $reference = SetReference::all()->first(function ($setReference) {
39 5
            return $setReference->key() == $this->key;
40 5
        });
41
42 5
        if (!$reference) {
43 1
            throw new \Exception('No query set found by key ['. $this->key. ']. Make sure that this '.$this->key.' set is added to the chief.sets config array.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 162 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...
44
        }
45
46 4
        return $reference;
47
    }
48
49 2
    public function flatReference(): FlatReference
50
    {
51 2
        return new FlatReference(static::class, $this->id);
52
    }
53
54 2
    public function flatReferenceLabel(): string
55
    {
56 2
        return $this->toReference()->flatReferenceLabel();
57
    }
58
59 2
    public function flatReferenceGroup(): string
60
    {
61 2
        return $this->toReference()->flatReferenceGroup();
62
    }
63
}
64