Test Failed
Push — ft/states ( 0e1fde...7e5f34 )
by Ben
08:31
created

ArchiveAssistant::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Thinktomorrow\Chief\Management\Assistants;
5
6
use Carbon\Carbon;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Collection;
9
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory;
10
use Thinktomorrow\Chief\States\State\StatefulContract;
11
use Thinktomorrow\Chief\Management\Application\ArchiveManagedModel;
12
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute;
13
use Thinktomorrow\Chief\Management\Manager;
14
use Thinktomorrow\Chief\Management\Managers;
15
use Thinktomorrow\Chief\Management\Application\UnarchiveManagedModel;
16
use Thinktomorrow\Chief\Urls\UrlRecord;
17
18
class ArchiveAssistant implements Assistant
19
{
20
    private $manager;
21
22
    private $model;
23
24 89
    /** @var Managers */
25
    private $managers;
26 89
27 89
    public function __construct(Managers $managers)
28
    {
29 89
        $this->managers = $managers;
30
    }
31 89
32 89
    public static function key(): string
33
    {
34 89
        return 'archive';
35
    }
36
37 89
    public function manager(Manager $manager)
38
    {
39 17
        $this->manager  = $manager;
40
        $this->model    = $manager->model();
41 17
42
        if(!$this->model instanceof StatefulContract){
43
            throw new \InvalidArgumentException('ArchiveAssistant requires the model to implement the StatefulContract.');
44
        }
45
    }
46
47
    public function isArchived(): bool
48
    {
49
        return $this->model->isArchived();
50
    }
51
52
    public function archivedAt(): Carbon
53
    {
54 4
        return new Carbon($this->model->archived_at);
55
    }
56 4
57
    public function findAllArchived(): Collection
58 4
    {
59 3
        return $this->model->archived()->get()->map(function ($model) {
60
            return $this->managers->findByModel($model);
61 2
        });
62
    }
63 2
64
    public function route($verb): ?string
65 2
    {
66 1
        $routes = [
67
            'index' => route('chief.back.assistants.archive-index', [$this->manager->details()->key]),
68 2
        ];
69
70
        if (array_key_exists($verb, $routes)) {
71
            return $routes[$verb] ?? null;
72 2
        }
73
74
        $modelRoutes = [
75 8
            'archive'   => route('chief.back.assistants.archive', [$this->manager->details()->key, $this->manager->model()->id]),
76
            'unarchive' => route('chief.back.assistants.unarchive', [$this->manager->details()->key, $this->manager->model()->id]),
77
        ];
78 8
79
        return isset($modelRoutes[$verb]) ? $modelRoutes[$verb] : null;
80
    }
81 8
82
    public function can($verb): bool
83
    {
84
        return !is_null($this->route($verb));
85
    }
86 8
87 8
    private function guard($verb): Assistant
88
    {
89
        if (! $this->can($verb)) {
90 8
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
91
        }
92
93 6
        return $this;
94
    }
95 6
96
    public function index(Request $request)
97
    {
98 6
        return view('chief::back.managers.archive.index', [
99
            'modelManager' => $this->manager,
100 6
            'managers' => $this->findAllArchived(),
101
        ]);
102
    }
103
104 6
    public function archive(Request $request)
105
    {
106
        $this->guard('archive');
107
108
        // If a redirect_id is passed along the request, it indicates the admin wants this page to be redirected to another one.
109
        if ($redirectReference = $request->get('redirect_id')) {
110
            $model = FlatReferenceFactory::fromString($redirectReference)->instance();
111
112
            $targetRecords = UrlRecord::getByModel($model);
113
114
            // Ok now get all urls from this model and point them to the new records
115
            foreach (UrlRecord::getByModel($this->manager->model()) as $urlRecord) {
116
                if ($targetRecord = $targetRecords->first(function ($record) use ($urlRecord) {
117
                    return ($record->locale == $urlRecord->locale && !$record->isRedirect());
118
                })) {
119
                    $urlRecord->redirectTo($targetRecord);
120
                }
121
            }
122
        }
123
124
        app(ArchiveManagedModel::class)->handle($this->model);
125
126
        return redirect()->to($this->manager->route('index'))->with('messages.success', $this->manager->details()->title .' is gearchiveerd.');
127
    }
128
129
    public function unarchive()
130
    {
131
        $this->guard('unarchive');
132
133
        app(UnArchiveManagedModel::class)->handle($this->model);
134
135
        return redirect()->to($this->manager->route('index'))->with('messages.success', $this->manager->details()->title .' is hersteld.');
136
    }
137
}
138