Completed
Push — master ( 381557...d947b3 )
by Ashley
01:11
created

Manager::checkHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
3
namespace Clarkeash\Shield;
4
5
use Clarkeash\Shield\Contracts\Service;
6
use Clarkeash\Shield\Exceptions\UnknownServiceException;
7
use Closure;
8
use Illuminate\Http\Request;
9
10
class Manager
11
{
12
    /**
13
     * @var \Illuminate\Support\Collection
14
     */
15
    protected $services;
16
17
    /**
18
     * Manager constructor.
19
     */
20
    public function __construct()
21
    {
22
        $this->services = collect();
23
24
        $this->load();
25
    }
26
27
    /**
28
     * Registers the enabled services
29
     */
30
    protected function load()
31
    {
32
        foreach (config('shield.enabled') as $name => $class) {
33
            $this->register($name, $class);
34
        }
35
    }
36
37
    /**
38
     * @param string $service
39
     *
40
     * @return bool
41
     */
42
    public function has(string $service)
43
    {
44
        return $this->services->has($service);
45
    }
46
47
    /**
48
     * @param string $service
49
     *
50
     * @return \Clarkeash\Shield\Contracts\Service | null
51
     */
52
    public function get(string $service)
53
    {
54
        return $this->services->get($service);
55
    }
56
57
    /**
58
     * @param string                              $service
59
     * @param \Clarkeash\Shield\Contracts\Service $instance
60
     */
61
    public function put(string $service, Service $instance)
62
    {
63
        $this->services->put($service, $instance);
64
    }
65
66
    /**
67
     * @param string                   $service
68
     * @param \Illuminate\Http\Request $request
69
     *
70
     * @return boolean
71
     */
72
    public function passes(string $service, Request $request): bool
73
    {
74
        throw_unless($this->has($service), UnknownServiceException::class, 'Service ['.$service.'] is not recognised.');
75
76
        if($this->checkHeaders($this->get($service)->headers(), $request)) {
77
            return $this->get($service)->verify($request);
78
        }
79
80
        return false;
81
    }
82
83
    public function checkHeaders(array $headers, Request $request)
84
    {
85
        foreach ($headers as $header) {
86
            if(!$request->hasHeader($header)) return false;
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * @param string $service
94
     * @param        $instance
95
     *
96
     * @throws \Clarkeash\Shield\Exceptions\UnknownServiceException
97
     */
98
    public function register(string $service, $instance)
99
    {
100
        if (is_string($instance) && class_exists($instance)) {
101
            $instance = app($instance);
102
        }
103
104
        if ($instance instanceof Closure) {
105
            $instance = $instance();
106
        }
107
108
        if (is_object($instance) && $instance instanceof Service) {
109
            return $this->put($service, $instance);
110
        }
111
112
        throw new UnknownServiceException('The service must be an instance of ' . Service::class);
113
    }
114
}
115