Test Failed
Push — ft/states ( ef32c7...d6dd80 )
by Ben
07:05 queued 19s
created

PageStatePresenter::stateAsLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Thinktomorrow\Chief\States;
4
5
use Thinktomorrow\Chief\Pages\Page;
6
use Thinktomorrow\Chief\States\State\StatefulContract;
7
8
class PageStatePresenter
9
{
10
    /** @var StatefulContract */
11
    private $model;
12
13
    /** @var PageState */
14
    private $pageState;
15
16
    public function __construct(StatefulContract $model, PageState $pageState)
17
    {
18
        $this->model = $model;
19
        $this->pageState = $pageState;
20
    }
21
22
    public static function fromModel(StatefulContract $model)
23
    {
24
        return new static($model, new PageState($model));
25
    }
26
27
    public function label(): string
28
    {
29
        $class = $this->pageState->isOnline() ? 'text-success' : 'text-warning';
30
31
        return '<span class="inline-xs stack-s '.$class.'">' . $this->stateAsLabel() . '</span>';
32
    }
33
34
    /**
35
     * Allowed transitions starting from this state
36
     * @return array
37
     */
38
    public function transitions(): array
39
    {
40
        return $this->pageState->allowedTransitions();
41
    }
42
43
    private function stateAsLabel()
44
    {
45
        $state = $this->model->state();
46
47
        $labels = [
48
            PageState::DRAFT => 'offline',
49
            PageState::PUBLISHED => 'online',
50
            PageState::ARCHIVED => 'archief',
51
        ];
52
53
        return $labels[$state] ?? $state;
54
    }
55
}
56