Passed
Push — master ( 3e205a...970265 )
by Philippe
66:06
created

ArchiveAssistant::manager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 5
    public function __construct(Managers $managers)
23
    {
24 5
        $this->managers = $managers;
25 5
    }
26
27 5
    public function manager(Manager $manager)
28
    {
29 5
        $this->manager  = $manager;
30 5
        $this->model    = $manager->model();
31 5
    }
32
33 51
    public static function key(): string
34
    {
35 51
        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
80
        $routes = [
81 4
            'index' => route('chief.back.assistants.archive-index', [$this->manager->details()->key]),
82
        ];
83
84 4
        if (array_key_exists($verb, $routes)) {
85
            return $routes[$verb] ?? null;
86
        }
87
88
        $modelRoutes = [
89 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...
90 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...
91
        ];
92
93 4
        return isset($modelRoutes[$verb]) ? $modelRoutes[$verb] : null;
94
    }
95
96 5
    public function can($verb): bool
97
    {
98
        try {
99 5
            $this->authorize($verb);
100 1
        } catch (NotAllowedManagerRoute $e) {
101 1
            return false;
102
        }
103
104 4
        return !is_null($this->route($verb));
105
    }
106
107
    /**
108
     * @param $verb
109
     * @throws NotAllowedManagerRoute
110
     */
111 5
    private function authorize($verb)
112
    {
113 5
        $permission = 'archive-page';
114
115 5
        if (! auth()->guard('chief')->user()->hasPermissionTo($permission)) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        if (! auth()->guard('chief')->user()->/** @scrutinizer ignore-call */ hasPermissionTo($permission)) {
Loading history...
116 1
            throw NotAllowedManagerRoute::notAllowedPermission($permission, $this->manager);
117
        }
118 4
    }
119
120 4
    public function guard($verb): Assistant
121
    {
122 4
        if (! $this->can($verb)) {
123 1
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
124
        }
125
126 3
        return $this;
127
    }
128
}
129