Passed
Push — master ( 970265...da43ab )
by Philippe
64:51 queued 33:01
created

ArchiveAssistant::authorize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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\Exceptions\NotAllowedManagerRoute;
10
use Thinktomorrow\Chief\Management\Manager;
11
use Thinktomorrow\Chief\Management\Managers;
12
13
class ArchiveAssistant implements Assistant
14
{
15
    private $manager;
16
17
    private $model;
18
19
    /** @var Managers */
20
    private $managers;
21
22 4
    public function __construct(Managers $managers)
23
    {
24 4
        $this->managers = $managers;
25 4
    }
26
27 4
    public function manager(Manager $manager)
28
    {
29 4
        $this->manager  = $manager;
30 4
        $this->model    = $manager->model();
31 4
    }
32
33 50
    public static function key(): string
34
    {
35 50
        return 'archive';
36
    }
37
38 4
    public function isArchived(): bool
39
    {
40 4
        return $this->model->isArchived();
41
    }
42
43
    public function archivedAt(): Carbon
44
    {
45
        return $this->model->archived_at;
46
    }
47
48 3
    public function archive()
49
    {
50 3
        $this->model->archive();
51
52 3
        Audit::activity()
53 3
            ->performedOn($this->model)
54 3
            ->log('archived');
55 3
    }
56
57
    public function unarchive()
58
    {
59
        $this->model->unarchive();
60
61
        if ($this->manager->isAssistedBy('publish')) {
62
            $this->model->draft();
63
        }
64
65
        Audit::activity()
66
            ->performedOn($this->model)
67
            ->log('unarchived');
68
    }
69
70
    public function findAll(): Collection
71
    {
72
        return $this->model->archived()->get()->map(function ($model) {
73
            return $this->managers->findByModel($model);
74
        });
75
    }
76
77 4
    public function route($verb): ?string
78
    {
79
        $routes = [
80 4
            'index' => route('chief.back.assistants.archive-index', [$this->manager->details()->key]),
81
        ];
82
83 4
        if (array_key_exists($verb, $routes)) {
84
            return $routes[$verb] ?? null;
85
        }
86
87
        $modelRoutes = [
88 4
            'archive'   => route('chief.back.assistants.archive', [$this->manager->details()->key, $this->manager->model()->id]),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
89 4
            'unarchive' => route('chief.back.assistants.unarchive', [$this->manager->details()->key, $this->manager->model()->id]),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
90
        ];
91
92 4
        return isset($modelRoutes[$verb]) ? $modelRoutes[$verb] : null;
93
    }
94
95 3
    public function can($verb): bool
96
    {
97 3
        return !is_null($this->route($verb));
98
    }
99
100 3
    public function guard($verb): Assistant
101
    {
102 3
        if (! $this->can($verb)) {
103
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
104
        }
105
106 3
        return $this;
107
    }
108
}
109