Passed
Push — master ( 7b35eb...39d150 )
by Philippe
01:23
created

Page::findOrCreateByKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Page extends Model
8
{
9
    public $table = 'squanto_pages';
10
11 24
    public static function make($key)
12
    {
13
        // TODO: assert unique key
14
        // TODO: sanitize and validate key
15
16 24
        $page = new self;
17 24
        $page->key = $key; // TODO: assert unique slug
18 24
        $page->label = ucfirst($key);
19 24
        $page->save();
20
21 24
        return $page;
22
    }
23
24 20
    public static function findOrCreateByKey($key)
25
    {
26 20
        if ($page = self::findByKey($key)) {
27 13
            return $page;
28
        }
29
30 19
        return self::make($key);
31
    }
32
33 24
    public static function findByKey($key)
34
    {
35 24
        return self::where('key', $key)->first();
36
    }
37
38 8
    public function lines()
39
    {
40 8
        return $this->hasMany(Line::class, 'page_id');
41
    }
42
43
    public static function getAll()
44
    {
45
        return self::sequence()->get();
46
    }
47
48
    public function scopeSequence($query)
49
    {
50
        return $query->orderBy('sequence', 'ASC');
51
    }
52
53 2
    public function isCompleted()
54
    {
55 2
        return Completion::check($this);
56
    }
57
58 2
    public function completionPercentage($locale)
59
    {
60 2
        return Completion::asPercentage($this, $locale);
61
    }
62
}
63