Completed
Pull Request — master (#39)
by Iman
09:05 queued 06:11
created

Consider::turn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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