Passed
Push — ft/states ( 728e57...af2f8a )
by Ben
08:47
created

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