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

Page   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 18
cts 22
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 12 1
A findOrCreateByKey() 0 8 2
A findByKey() 0 4 1
A getAll() 0 4 1
A scopeSequence() 0 4 1
A lines() 0 4 1
A isCompleted() 0 4 1
A completionPercentage() 0 4 1
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