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

Page::isCompleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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