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

PreviewMode::default()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 2
nop 0
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