Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

ChiefResponse::fromSlug()   B

Complexity

Conditions 9
Paths 56

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.0117

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 19
c 2
b 1
f 0
dl 0
loc 41
ccs 18
cts 19
cp 0.9474
rs 8.0555
cc 9
nc 56
nop 2
crap 9.0117
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Thinktomorrow\Chief\Concerns\Morphable\Morphables;
10
use Thinktomorrow\Chief\Concerns\Morphable\NotFoundMorphKey;
11
use Thinktomorrow\Chief\Concerns\Publishable\PreviewMode;
12
13
class ChiefResponse extends Response
14
{
15
//    public static function fromRequest(Request $request = null, $locale = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
//    {
17
//        if (!$request) {
18
//            $request = request();
19
//        }
20
//        if (!$locale) {
21
//            $locale = app()->getLocale();
22
//        }
23
//
24
//        return static::fromSlug($request->path(), $locale);
25
//    }
26
27 11
    public static function fromSlug(string $slug, $locale = null)
28
    {
29 11
        if (!$locale) {
30 8
            $locale = app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

30
            $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
31
        }
32
33
        try {
34 11
            $urlRecord = UrlRecord::findBySlug($slug, $locale);
35
36 9
            $model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id);
37
38 8
            if ($urlRecord->isRedirect()) {
39
40
                // If model is not found, it probably means it is archived or removed
41
                // So we detect the model based on the redirect target url.
42 3
                if (!$model) {
43 1
                    $targetUrlRecord = $urlRecord->redirectTo();
44 1
                    $targetModel = Morphables::instance($targetUrlRecord->model_type)->find($targetUrlRecord->model_id);
45
46 1
                    return static::createRedirect($targetModel->url($locale));
47
                }
48
49 2
                return static::createRedirect($model->url($locale));
50
            }
51
52 5
            if (method_exists($model, 'isPublished') && ! $model->isPublished()) {
53
54
                /** When admin is logged in and this request is in preview mode, we allow the view */
55 3
                if (! PreviewMode::fromRequest()->check()) {
56 2
                    throw new NotFoundHttpException('Model found for request ['. $slug .'] but it is not published.');
57
                }
58
            }
59
60 3
            return new static($model->renderView(), 200);
61 5
        } catch (UrlRecordNotFound | NotFoundMorphKey $e) {
62 3
            if (config('thinktomorrow.chief.strict')) {
63
                throw $e;
64
            }
65
        }
66
67 3
        throw new NotFoundHttpException('No url or model found for request ['. $slug .'] for locale ['.$locale.'].');
68
    }
69
70 3
    private static function createRedirect(string $url)
71
    {
72 3
        return new RedirectResponse($url, 301, []);
73
    }
74
}
75