Manager::checkHeaders()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Shield\Shield;
4
5
use Illuminate\Support\Arr;
6
use Shield\Shield\Contracts\Service;
7
use Shield\Shield\Exceptions\UnknownServiceException;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Shield\Shield\Exceptions\UnsupportedDriverException;
11
12
class Manager
13
{
14 7
    public function passes(string $service, Request $request): bool
15
    {
16
        /** @var Service $instance */
17 7
        $instance = $this->validate($service);
18
19 4
        if ($this->checkHeaders($instance->headers(), $request)) {
20 3
            return $instance->verify($request, collect(config('shield.services.' . $service . '.options', [])));
21
        }
22
23 1
        return false;
24
    }
25
26
    /**
27
     * @param string $service
28
     *
29
     * @return Service
30
     */
31 7
    protected function validate(string $service)
32
    {
33 7
        throw_unless(Arr::exists(config('shield.services'), $service), UnknownServiceException::class, sprintf('Service [%s] not found.', $service));
34 6
        throw_unless(Arr::exists(config('shield.services.' . $service), 'driver'), UnknownServiceException::class, sprintf('Service [%s] must have a driver.', $service));
35
36 5
        $service = app(config('shield.services.' . $service . '.driver'));
37
38 5
        throw_unless($service instanceof Service, UnsupportedDriverException::class, sprintf('Driver [%s] must implement [%s].', get_class($service), Service::class));
39
40 4
        return $service;
41
    }
42
43 4
    public function checkHeaders(array $headers, Request $request)
44
    {
45 4
        foreach ($headers as $header) {
46 2
            if (!$request->hasHeader($header)) return false;
47
        }
48
49 3
        return true;
50
    }
51
}
52