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

PageStatePresenter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 37
rs 10

4 Methods

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