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

PageStatePresenter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 46
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromModel() 0 3 1
A stateAsLabel() 0 11 1
A label() 0 5 2
A transitions() 0 3 1
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