Passed
Pull Request — master (#83)
by Бабичев
09:17
created

LockService::lockProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Services;
4
5
use Bavix\Wallet\Objects\EmptyLock;
6
use Illuminate\Contracts\Cache\Lock;
7
use Illuminate\Contracts\Cache\LockProvider;
8
use Illuminate\Support\Facades\Cache;
9
10
class LockService
11
{
12
13
    /**
14
     * @param object $self
15
     * @param string $name
16
     * @param \Closure $closure
17
     * @return mixed
18
     */
19 50
    public function lock($self, string $name, \Closure $closure)
20
    {
21 50
        return $this->lockProvider($name, (int)config('wallet.lock.seconds'))
22 50
            ->get($this->bindTo($self, $closure));
23
    }
24
25
    /**
26
     * @param object $self
27
     * @param \Closure $closure
28
     * @return \Closure
29
     */
30 50
    protected function bindTo($self, \Closure $closure): \Closure
31
    {
32
        try {
33 50
            return $closure->bindTo($self);
34 47
        } catch (\Throwable $throwable) {
35 47
            return $closure;
36
        }
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param int $seconds
42
     * @return Lock
43
     */
44 50
    protected function lockProvider(string $name, int $seconds): Lock
45
    {
46
        /**
47
         * @var LockProvider $store
48
         */
49 50
        $store = Cache::getStore();
50
51 50
        if ($store instanceof LockProvider) {
0 ignored issues
show
introduced by
$store is always a sub-type of Illuminate\Contracts\Cache\LockProvider.
Loading history...
52
            return $store->lock($name, $seconds);
53
        }
54
55 50
        return new EmptyLock();
56
    }
57
58
}
59