Passed
Push — 0.3 ( 9cb195...9cd874 )
by Ben
69:50 queued 33:49
created

PublishAssistant::hasPreviewUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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