Passed
Push — ft/fields-refactor ( 34e13a...220f3b )
by Ben
81:47
created

RevertUrlSlug::handle()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 15
ccs 6
cts 7
cp 0.8571
rs 10
c 1
b 0
f 0
cc 3
nc 6
nop 1
crap 3.0261
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls\Application;
4
5
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl;
6
use Thinktomorrow\Chief\Urls\UrlRecord;
7
use Thinktomorrow\Chief\Urls\UrlRecordNotFound;
8
9
/**
10
 * Revert slug to most recent redirect or empty it when no redirect exists.
11
 */
12
class RevertUrlSlug
13
{
14
    /** @var ProvidesUrl */
15
    private $model;
16
17
    private $existingRecords;
0 ignored issues
show
introduced by
The private property $existingRecords is not used, and could be removed.
Loading history...
18
19 1
    public function __construct(ProvidesUrl $model)
20
    {
21 1
        $this->model = $model;
22 1
    }
23
24 1
    public function handle(string $locale): void
25
    {
26
        try{
27 1
            $currentUrlRecord = UrlRecord::findByModel($this->model, $locale);
28
29 1
            if($recentRedirect = UrlRecord::findRecentRedirect($this->model, $locale)){
30
31 1
                $recentRedirectSlug = $recentRedirect->slug;
32 1
                $recentRedirect->delete();
33
34 1
                $currentUrlRecord->replaceAndRedirect(['slug' => $recentRedirectSlug]);
35
            }
36
        }
37
        catch(UrlRecordNotFound $e)
38
        {
39
            // No url present so nothing to do here...
40
        }
41 1
    }
42
}
43