Completed
Push — ft/states ( 7e359d...6efccf )
by Philippe
69:12 queued 61:47
created

PageState::make()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\States;
4
5
use Thinktomorrow\Chief\States\State\StatefulContract;
6
use Thinktomorrow\Chief\States\State\StateMachine;
7
8
class PageState extends StateMachine
9
{
10
    // column key that refers to the current state in db
11
    const KEY = 'current_state';
12
13
    // Offline states
14
    const DRAFT = 'draft';
15
    const ARCHIVED = 'archived';
16
    const DELETED = 'deleted'; // soft deleted
17
18
    // Online states
19
    const PUBLISHED = 'published';
20
21
    protected $states = [
22
        self::DRAFT,
23
        self::ARCHIVED,
24
        self::DELETED,
25
26
        self::PUBLISHED,
27
    ];
28
29
    protected $transitions = [
30
        'publish' => [
31
            'from' => [self::DRAFT],
32
            'to'   => self::PUBLISHED,
33
        ],
34
        'unpublish' => [
35
            'from' => [self::PUBLISHED],
36
            'to'   => self::DRAFT,
37
        ],
38
        'archive' => [
39
            'from' => [self::PUBLISHED, self::DRAFT],
40
            'to'   => self::ARCHIVED,
41
        ],
42
        'unarchive' => [
43
            'from' => [self::ARCHIVED],
44
            'to'   => self::DRAFT,
45
        ],
46
        'delete' => [
47
            'from' => [self::ARCHIVED, self::DRAFT],
48
            'to'   => self::DELETED,
49
        ],
50
    ];
51
52 87
    public static function make(StatefulContract $model)
53
    {
54 87
        return new static($model, static::KEY);
55
    }
56
57 1
    public function isOffline(): bool
58
    {
59 1
        return in_array($this->statefulContract->stateOf(static::KEY), [
60 1
            static::DRAFT,
61 1
            static::ARCHIVED,
62 1
            static::DELETED,
63
        ]);
64
    }
65
66 80
    public function isOnline(): bool
67
    {
68 80
        return in_array($this->statefulContract->stateOf(static::KEY), [
69 80
            static::PUBLISHED,
70
        ]);
71
    }
72
}
73