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

PublishAssistant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Thinktomorrow\Chief\Management\Assistants;
5
6
use Carbon\Carbon;
7
use Illuminate\Support\Collection;
8
use Thinktomorrow\Chief\Audit\Audit;
9
use Thinktomorrow\Chief\Management\Manager;
10
use Thinktomorrow\Chief\Management\Managers;
11
use Thinktomorrow\Chief\Management\Application\PublishManagedModel;
12
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute;
13
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl;
14
use Thinktomorrow\Chief\Management\Application\UnpublishManagedModel;
15
16
class PublishAssistant implements Assistant
17
{
18
    private $manager;
19
20
    private $model;
21
22
    /** @var Managers */
23
    private $managers;
24
25
    public function __construct(Managers $managers)
26
    {
27
        $this->managers = $managers;
28
    }
29
30
    public function manager(Manager $manager)
31
    {
32
        $this->manager = $manager;
33
        $this->model = $manager->model();
34
    }
35
36
    public static function key(): string
37
    {
38
        return 'publish';
39
    }
40
41
    public function isPublished(): bool
42
    {
43
        return $this->model->isPublished();
44
    }
45
46
    public function isDraft(): bool
47
    {
48
        return $this->model->isDraft();
49
    }
50
51
    public function publishedAt(): Carbon
52
    {
53
        return $this->model->Published_at;
54
    }
55
56
    public function publish()
57
    {
58
        $this->guard('publish');
59
60
        app(PublishManagedModel::class)->handle($this->model);
61
    }
62
63
    public function unpublish()
64
    {
65
        $this->guard('unpublish');
66
67
        app(UnpublishManagedModel::class)->handle($this->model);
68
    }
69
70
    public function findAll(): Collection
71
    {
72
        return $this->model->published()->get()->map(function ($model) {
73
            return $this->managers->findByModel($model);
74
        });
75
    }
76
77
    public function route($verb): ?string
78
    {
79
        $modelRoutes = [
80
            'publish'   => route('chief.back.assistants.publish', [$this->manager->details()->key, $this->manager->model()->id]),
81
            'unpublish'     => route('chief.back.assistants.unpublish', [$this->manager->details()->key, $this->manager->model()->id]),
82
        ];
83
84
        return $modelRoutes[$verb] ?? null;
85
    }
86
87
    public function can($verb): bool
88
    {
89
        return !is_null($this->route($verb));
90
    }
91
92
    public function guard($verb): Assistant
93
    {
94
        if (! $this->can($verb)) {
95
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
96
        }
97
98
        return $this;
99
    }
100
101
    public function hasPreviewUrl(): bool
102
    {
103
        return $this->model instanceof ProvidesUrl && $this->previewUrl() != '?preview-mode';
104
    }
105
106
    public function publicationStatusAsLabel($plain = false)
107
    {
108
        $label = $this->publicationStatusAsPlainLabel();
109
        $class = '';
110
111
        if ($this->isPublished()) {
112
            $class = 'text-success';
113
        } elseif ($this->isDraft()) {
114
            $class = 'text-error';
115
        } elseif ($this->manager->isAssistedBy('archive') && $this->manager->assistant('archive')->isArchived()) {
116
            $class = 'text-warning';
117
        }
118
119
        $statusAsLabel = '<span class="font-bold '. $class .'"><em>' . $label . '</em></span>';
120
121
        if (!$plain && $this->hasPreviewUrl()) {
122
            $statusAsLabel =  '<a href="'.$this->previewUrl().'" target="_blank">'. $statusAsLabel .'</a>';
123
        }
124
125
        return $statusAsLabel;
126
    }
127
128
    private function publicationStatusAsPlainLabel()
129
    {
130
        if ($this->isPublished()) {
131
            return 'online';
132
        } elseif ($this->isDraft()) {
133
            return 'offline';
134
        } elseif ($this->manager->isAssistedBy('archive') && $this->manager->assistant('archive')->isArchived()) {
135
            return 'gearchiveerd';
136
        }
137
138
        return '-';
139
    }
140
141
    public function previewUrl(): string
142
    {
143
        return $this->model->previewUrl();
144
    }
145
}
146