Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

ArchiveController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 57
c 0
b 0
f 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A unarchive() 0 9 1
A archive() 0 24 5
A index() 0 9 1
A __construct() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\Assistants;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Chief\FlatReferences\FlatReferenceCollection;
7
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory;
8
use Thinktomorrow\Chief\Management\Managers;
9
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
10
use Thinktomorrow\Chief\Urls\UrlRecord;
11
12
class ArchiveController extends Controller
13
{
14
    /** @var Managers */
15
    private $managers;
16
17
    public function __construct(Managers $managers)
18
    {
19
        $this->managers = $managers;
20
    }
21
22
    public function index(Request $request, string $key)
23
    {
24
        $manager = $this->managers->findByKey($key);
25
26
        $managers = $manager->assistant('archive')->findAll();
0 ignored issues
show
Bug introduced by
The method findAll() does not exist on Thinktomorrow\Chief\Mana...nt\Assistants\Assistant. It seems like you code against a sub-type of Thinktomorrow\Chief\Mana...nt\Assistants\Assistant such as Thinktomorrow\Chief\Mana...stants\PublishAssistant or Thinktomorrow\Chief\Mana...stants\ArchiveAssistant. ( Ignorable by Annotation )

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

26
        $managers = $manager->assistant('archive')->/** @scrutinizer ignore-call */ findAll();
Loading history...
27
28
        return view('chief::back.managers.archive.index', [
29
            'modelManager' => $manager,
30
            'managers' => $managers,
31
        ]);
32
    }
33
34
    public function archive(Request $request, $key, $id)
35
    {
36
        $manager = $this->managers->findByKey($key, $id);
37
38
        if ($redirectReference = $request->get('redirect_id')) {
39
            $model = FlatReferenceFactory::fromString($redirectReference)->instance();
40
41
            $targetRecords = UrlRecord::getByModel($model);
42
43
            // Ok now get all urls from this model and point them to the new records
44
            foreach (UrlRecord::getByModel($manager->model()) as $urlRecord) {
45
                if ($targetRecord = $targetRecords->first(function ($record) use ($urlRecord) {
46
                    return ($record->locale == $urlRecord->locale && !$record->isRedirect());
47
                })) {
48
                    $urlRecord->redirectTo($targetRecord);
49
                }
50
            }
51
        }
52
53
        $manager->assistant('archive')
54
                        ->guard('archive')
55
                        ->archive();
0 ignored issues
show
Bug introduced by
The method archive() does not exist on Thinktomorrow\Chief\Mana...nt\Assistants\Assistant. It seems like you code against a sub-type of Thinktomorrow\Chief\Mana...nt\Assistants\Assistant such as Thinktomorrow\Chief\Mana...stants\ArchiveAssistant. ( Ignorable by Annotation )

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

55
                        ->/** @scrutinizer ignore-call */ archive();
Loading history...
56
57
        return redirect()->to($manager->route('index'))->with('messages.success', $manager->details()->title .' is gearchiveerd.');
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist on Thinktomorrow\Chief\Management\Details\Details. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
    }
59
60
    public function unarchive($key, $id)
61
    {
62
        $manager = $this->managers->findByKey($key, $id);
63
64
        $manager->assistant('archive')
65
            ->guard('unarchive')
66
            ->unarchive();
0 ignored issues
show
Bug introduced by
The method unarchive() does not exist on Thinktomorrow\Chief\Mana...nt\Assistants\Assistant. It seems like you code against a sub-type of Thinktomorrow\Chief\Mana...nt\Assistants\Assistant such as Thinktomorrow\Chief\Mana...stants\ArchiveAssistant. ( Ignorable by Annotation )

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

66
            ->/** @scrutinizer ignore-call */ unarchive();
Loading history...
67
68
        return redirect()->to($manager->route('index'))->with('messages.success', $manager->details()->title .' is hersteld.');
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist on Thinktomorrow\Chief\Management\Details\Details. Since you implemented __get, consider adding a @property annotation.
Loading history...
69
    }
70
}
71