Completed
Push — master ( a52e49...d03ec2 )
by Iman
09:53
created

Consider::allChecks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\Switching;
4
5
use Imanghafoori\HeyMan\WatchingStrategies\{EloquentEventsManager, EventManager, RouterEventManager, ViewEventManager};
6
7
class Consider
8
{
9
    private $mode;
10
11
    public function __construct($mode)
12
    {
13
        $this->mode = $mode;
14
    }
15
16
    public function eloquentChecks(callable $closure = null)
17
    {
18
        return $this->turn(EloquentEventsManager::class, $closure);
19
    }
20
21
    public function viewChecks(callable $closure = null)
22
    {
23
        return $this->turn(ViewEventManager::class, $closure);
24
    }
25
26
    public function routeChecks(callable $closure = null)
27
    {
28
        return $this->turn(RouterEventManager::class, $closure);
29
    }
30
31
    public function eventChecks(callable $closure = null)
32
    {
33
        return $this->turn(EventManager::class, $closure);
34
    }
35
36
    public function validationChecks(callable $closure = null)
37
    {
38
        return $this->turn('validation', $closure);
39
    }
40
41
    public function allChecks()
42
    {
43
        $this->validationChecks();
44
        $this->eventChecks();
45
        $this->eloquentChecks();
46
        $this->routeChecks();
47
        $this->viewChecks();
48
    }
49
50
    /**
51
     * @param $key
52
     * @param callable|null $closure
53
     */
54
    private function turn($key, callable $closure = null)
55
    {
56
        $key = 'heyman_ignore_'.$key;
57
58
        $current = config($key);
59
        $this->changeMode($key);
60
61
        if (is_null($closure)) {
62
            return;
63
        }
64
        $result = $closure();
65
        config()->set($key, $current);
66
67
        return $result;
68
    }
69
70
    /**
71
     * @param $key
72
     */
73
    private function changeMode($key)
74
    {
75
        config()->set($key, [
76
            'turnOff' => true,
77
            'turnOn'  => false,
78
        ][$this->mode]);
79
    }
80
}
81