Test Failed
Branch master (52949b)
by Artem
05:53
created

ResolveTenant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Slides\Saml2\Http\Middleware;
4
5
use Slides\Saml2\Repositories\TenantRepository;
6
use Illuminate\Support\Facades\Log;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...n\NotFoundHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Slides\Saml2\OneLoginBuilder;
9
10
/**
11
 * Class ResolveTenant
12
 *
13
 * @package Slides\Saml2\Http\Middleware
14
 */
15
class ResolveTenant
16
{
17
    /**
18
     * @var TenantRepository
19
     */
20
    protected $tenants;
21
22
    /**
23
     * @var OneLoginBuilder
24
     */
25
    protected $builder;
26
27
    /**
28
     * ResolveTenant constructor.
29
     *
30
     * @param TenantRepository $tenants
31
     * @param OneLoginBuilder $builder
32
     */
33
    public function __construct(TenantRepository $tenants, OneLoginBuilder $builder)
34
    {
35
        $this->tenants = $tenants;
36
        $this->builder = $builder;
37
    }
38
39
    /**
40
     * Handle an incoming request.
41
     *
42
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     * @param  \Closure  $next
44
     *
45
     * @throws NotFoundHttpException
46
     *
47
     * @return mixed
48
     */
49
    public function handle($request, \Closure $next)
50
    {
51
        if(!$tenant = $this->resolveTenant($request)) {
52
            throw new NotFoundHttpException();
53
        }
54
55
        Log::debug('[Saml2] Tenant resolved', [
56
            'uuid' => $tenant->uuid,
57
            'id' => $tenant->id,
58
            'key' => $tenant->key
59
        ]);
60
61
        session()->flash('saml2.tenant.uuid', $tenant->uuid);
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        /** @scrutinizer ignore-call */ 
62
        session()->flash('saml2.tenant.uuid', $tenant->uuid);
Loading history...
62
63
        $this->builder
64
            ->withTenant($tenant)
65
            ->bootstrap();
66
67
        return $next($request);
68
    }
69
70
    /**
71
     * Resolve a tenant by a request.
72
     *
73
     * @param  \Illuminate\Http\Request  $request
74
     *
75
     * @return \Slides\Saml2\Models\Tenant|null
76
     */
77
    protected function resolveTenant($request)
78
    {
79
        if(!$uuid = $request->route('uuid')) {
80
            Log::debug('[Saml2] Tenant UUID is not present in the URL so cannot be resolved', [
81
                'url' => $request->fullUrl()
82
            ]);
83
84
            return null;
85
        }
86
87
        if(!$tenant = $this->tenants->findByUUID($uuid)) {
88
            Log::debug('[Saml2] Tenant doesn\'t exist', [
89
                'uuid' => $uuid
90
            ]);
91
92
            return null;
93
        }
94
95
        if($tenant->trashed()) {
96
            Log::debug('[Saml2] Tenant #' . $tenant->id. ' resolved but marked as deleted', [
97
                'id' => $tenant->id,
98
                'uuid' => $uuid,
99
                'deleted_at' => $tenant->deleted_at->toDateTimeString()
100
            ]);
101
102
            return null;
103
        }
104
105
        return $tenant;
106
    }
107
}