Test Failed
Push — ft/states ( eebc9c )
by Ben
29:42 queued 08:49
created

PageStatePresenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
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 render(): string
28
    {
29
        $class = $this->pageState->isOnline() ? 'text-success' : 'text-warning';
30
31
        return '<span class="inline-xs stack-s '.$class.'">' . $this->label() . '</span>';
32
    }
33
34
    private function label()
35
    {
36
        $state = $this->model->state();
37
38
        $labels = [
39
            PageState::DRAFT => 'offline',
40
            PageState::PUBLISHED => 'online',
41
            PageState::ARCHIVED => 'archief',
42
        ];
43
44
        return $labels[$state] ?? $state;
45
    }
46
}
47