TenantRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 97
ccs 0
cts 27
cp 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findById() 0 5 1
A findByKey() 0 5 1
A findByAnyIdentifier() 0 11 2
A all() 0 3 1
A findByUUID() 0 5 1
A query() 0 10 2
1
<?php
2
3
namespace Slides\Saml2\Repositories;
4
5
use Slides\Saml2\Models\Tenant;
6
7
/**
8
 * Class TenantRepository
9
 *
10
 * @package Slides\Saml2\Repositories
11
 */
12
class TenantRepository
13
{
14
    /**
15
     * Create a new query.
16
     *
17
     * @param bool $withTrashed Whether need to include safely deleted records.
18
     *
19
     * @return \Illuminate\Database\Eloquent\Builder
20
     */
21
    public function query(bool $withTrashed = false)
22
    {
23
        $class = config('saml2.tenantModel', Tenant::class);
0 ignored issues
show
Bug introduced by
The function config 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

23
        $class = /** @scrutinizer ignore-call */ config('saml2.tenantModel', Tenant::class);
Loading history...
24
        $query = $class::query();
25
26
        if($withTrashed) {
27
            $query->withTrashed();
28
        }
29
30
        return $query;
31
    }
32
33
    /**
34
     * Find all tenants.
35
     *
36
     * @param bool $withTrashed Whether need to include safely deleted records.
37
     *
38
     * @return Tenant[]|\Illuminate\Database\Eloquent\Collection
39
     */
40
    public function all(bool $withTrashed = true)
41
    {
42
        return $this->query($withTrashed)->get();
43
    }
44
45
    /**
46
     * Find a tenant by any identifier.
47
     *
48
     * @param int|string $key ID, key or UUID
49
     * @param bool $withTrashed Whether need to include safely deleted records.
50
     *
51
     * @return Tenant[]|\Illuminate\Database\Eloquent\Collection
52
     */
53
    public function findByAnyIdentifier($key, bool $withTrashed = true)
54
    {
55
        $query = $this->query($withTrashed);
56
57
        if (is_int($key)) {
58
            return $query->where('id', $key)->get();
59
        }
60
61
        return $query->where('key', $key)
62
            ->orWhere('uuid', $key)
63
            ->get();
64
    }
65
66
    /**
67
     * Find a tenant by the key.
68
     *
69
     * @param string $key
70
     * @param bool $withTrashed
71
     *
72
     * @return Tenant|\Illuminate\Database\Eloquent\Model|null
73
     */
74
    public function findByKey(string $key, bool $withTrashed = true)
75
    {
76
        return $this->query($withTrashed)
77
            ->where('key', $key)
78
            ->first();
79
    }
80
81
    /**
82
     * Find a tenant by ID.
83
     *
84
     * @param int $id
85
     * @param bool $withTrashed
86
     *
87
     * @return Tenant|\Illuminate\Database\Eloquent\Model|null
88
     */
89
    public function findById(int $id, bool $withTrashed = true)
90
    {
91
        return $this->query($withTrashed)
92
            ->where('id', $id)
93
            ->first();
94
    }
95
96
    /**
97
     * Find a tenant by UUID.
98
     *
99
     * @param int $uuid
100
     * @param bool $withTrashed
101
     *
102
     * @return Tenant|\Illuminate\Database\Eloquent\Model|null
103
     */
104
    public function findByUUID(string $uuid, bool $withTrashed = true)
105
    {
106
        return $this->query($withTrashed)
107
            ->where('uuid', $uuid)
108
            ->first();
109
    }
110
}