Passed
Push — 0.3 ( a188cc...3c900f )
by Philippe
27:26
created

ArchiveAssistant::archive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
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\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 71
    public function __construct(Managers $managers)
23
    {
24 71
        $this->managers = $managers;
25 71
    }
26
27 71
    public function manager(Manager $manager)
28
    {
29 71
        $this->manager  = $manager;
30 71
        $this->model    = $manager->model();
31 71
    }
32
33 57
    public static function key(): string
34
    {
35 57
        return 'archive';
36
    }
37
38 6
    public function isArchived(): bool
39
    {
40 6
        return $this->model->isArchived();
41
    }
42
43
    public function archivedAt(): Carbon
44
    {
45
        return $this->model->archived_at;
46
    }
47
48 4
    public function archive()
49
    {
50 4
        $this->model->archive();
51
52 4
        Audit::activity()
53 4
            ->performedOn($this->model)
54 4
            ->log('archived');
55 4
    }
56
57 1
    public function unarchive()
58
    {
59 1
        $this->model->unarchive();
60
61 1
        if ($this->manager->isAssistedBy('publish')) {
62 1
            $this->model->draft();
63
        }
64
65 1
        Audit::activity()
66 1
            ->performedOn($this->model)
67 1
            ->log('unarchived');
68 1
    }
69
70 2
    public function findAll(): Collection
71
    {
72
        return $this->model->archived()->get()->map(function ($model) {
73
            return $this->managers->findByModel($model);
74 2
        });
75
    }
76
77 7
    public function route($verb): ?string
78
    {
79
        $routes = [
80 7
            'index' => route('chief.back.assistants.archive-index', [$this->manager->details()->key]),
81
        ];
82
83 7
        if (array_key_exists($verb, $routes)) {
84
            return $routes[$verb] ?? null;
85
        }
86
87
        $modelRoutes = [
88 7
            'archive'   => route('chief.back.assistants.archive', [$this->manager->details()->key, $this->manager->model()->id]),
89 7
            'unarchive' => route('chief.back.assistants.unarchive', [$this->manager->details()->key, $this->manager->model()->id]),
90
        ];
91
92 7
        return isset($modelRoutes[$verb]) ? $modelRoutes[$verb] : null;
93
    }
94
95 5
    public function can($verb): bool
96
    {
97 5
        return !is_null($this->route($verb));
98
    }
99
100 5
    public function guard($verb): Assistant
101
    {
102 5
        if (! $this->can($verb)) {
103
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
104
        }
105
106 5
        return $this;
107
    }
108
}
109