Test Setup Failed
Push — dependabot/composer/phpunit/ph... ( 94e118...04557f )
by
unknown
76:41 queued 69:49
created

PreviewMode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 13
c 3
b 0
f 0
dl 0
loc 37
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A toggle() 0 3 1
A fromRequest() 0 9 4
A default() 0 7 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\States\Publishable;
6
7
use Illuminate\Support\Str;
8
9
class PreviewMode
10
{
11
    private $active;
12
13
    final public function __construct(bool $active)
14
    {
15
        $this->active = $active;
16
    }
17
18
    public static function fromRequest()
19
    {
20
        if(!config('chief.preview-mode') || Str::startsWith(request()->path(), 'admin/')) {
21
            return new static(false);
22
        }
23
24
        $active = (session()->get('preview-mode', static::default()) === true && auth()->guard('chief')->check());
25
26
        return new static($active);
27
    }
28
29
    public static function toggle()
30
    {
31
        session()->put('preview-mode', !session()->get('preview-mode', static::default()));
32
    }
33
34
    public function check(): bool
35
    {
36
        return $this->active;
37
    }
38
39
    private static function default(): bool
40
    {
41
        $mode = config('chief.preview-mode');
42
43
        if(!$mode || $mode == 'live') return false;
44
45
        return ($mode == 'preview');
46
    }
47
}
48