TenantRepository::update()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AtlassianConnectCore\Repositories;
4
5
use AtlassianConnectCore\Models\Tenant;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8
/**
9
 * Class TenantRepository
10
 *
11
 * @package AtlassianConnectCore\Repositories
12
 */
13
class TenantRepository
14
{
15
    /**
16
     * Create a query instance
17
     *
18
     * @param bool $withTrashed
19
     *
20
     * @return \Illuminate\Database\Eloquent\Builder
21
     */
22
    protected function query($withTrashed = true)
23
    {
24
        $query = Tenant::query();
25
26
        if($withTrashed) {
27
            $query->withTrashed();
28
        }
29
30
        return $query;
31
    }
32
33
    /**
34
     * Find by ID
35
     *
36
     * @param int $id
37
     *
38
     * @return Tenant|null
39
     */
40
    public function findById($id)
41
    {
42
        /** @var Tenant|null $model */
43
        $model = $this->query()
44
            ->find($id);
45
46
        return $model;
47
    }
48
49
    /**
50
     * Find all tenants
51
     *
52
     * @param bool $withTrashed
53
     *
54
     * @return \Illuminate\Database\Eloquent\Collection|Tenant[]
55
     */
56
    public function findAll($withTrashed = false)
57
    {
58
        return $this->query($withTrashed)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query($wit...ashed)->latest()->get() returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type AtlassianConnectCore\Mod...ase\Eloquent\Collection.
Loading history...
59
            ->latest()
60
            ->get();
61
    }
62
63
    /**
64
     * Find by ID
65
     *
66
     * @param array $condition
67
     * @param bool $withTrashed
68
     *
69
     * @return \Illuminate\Database\Eloquent\Collection|Tenant[]
70
     */
71
    public function findWhere(array $condition, $withTrashed = true)
72
    {
73
        return $this->query($withTrashed)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query($wit...here($condition)->get() returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type AtlassianConnectCore\Mod...ase\Eloquent\Collection.
Loading history...
74
            ->where($condition)
0 ignored issues
show
Security introduced by
$condition can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. ParameterBag::get() returns request data
    in vendor/symfony/http-foundation/ParameterBag.php on line 86
  2. $this->attributes->get($key, $this) is assigned to $result
    in vendor/symfony/http-foundation/Request.php on line 808
  3. TenantService::findByClientKeyOrFail() is called
    in src/Http/Controllers/TenantController.php on line 56
  4. Enters via parameter $clientKey
    in src/Services/TenantService.php on line 102
  5. TenantService::findByClientKey() is called
    in src/Services/TenantService.php on line 104
  6. Enters via parameter $clientKey
    in src/Services/TenantService.php on line 85
  7. TenantRepository::findWhere() is called
    in src/Services/TenantService.php on line 88
  8. Enters via parameter $condition
    in src/Repositories/TenantRepository.php on line 71

Used in code-execution context

  1. Builder::where() is called
    in src/Repositories/TenantRepository.php on line 74
  2. Enters via parameter $column
    in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 219
  3. $column() is called dynamically
    in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 224

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
75
            ->get();
76
    }
77
78
    /**
79
     * Find dummy tenant
80
     *
81
     * @return Tenant|null
82
     */
83
    public function findDummy()
84
    {
85
        /** @var Tenant|null $model */
86
        $model = $this->query(false)
87
            ->where(['is_dummy' => true])
88
            ->latest()
89
            ->first();
90
91
        return $model;
92
    }
93
94
    /**
95
     * Create a tenant
96
     *
97
     * @param array $attributes
98
     *
99
     * @return Tenant
100
     */
101
    public function create(array $attributes)
102
    {
103
        $model = (new Tenant())->fill($attributes);
104
105
        $model->save();
106
107
        return $model;
108
    }
109
110
    /**
111
     * Update a tenant
112
     *
113
     * @param int $id
114
     * @param array $attributes
115
     *
116
     * @return Tenant
117
     */
118
    public function update($id, array $attributes)
119
    {
120
        $model = $this->findById($id);
121
122
        if(!$model) {
123
            throw new NotFoundHttpException();
124
        }
125
126
        $model->update($attributes);
127
128
        return $model;
129
    }
130
131
    /**
132
     * Update a tenant by addon key
133
     *
134
     * @param string $addonKey
135
     * @param array $attributes
136
     *
137
     * @return Tenant
138
     */
139
    public function updateByAddonKey($addonKey, array $attributes)
140
    {
141
        $model = $this->findWhere(['addon_key' => $addonKey])->first();
142
143
        if(!$model) {
144
            throw new NotFoundHttpException();
145
        }
146
147
        return $this->update($model->id, $attributes);
148
    }
149
150
    /**
151
     * Update a tenant by addon key
152
     *
153
     * @param array $condition
154
     * @param array $attributes
155
     *
156
     * @return Tenant
157
     */
158
    public function updateWhere(array $condition, array $attributes)
159
    {
160
        $model = $this->findWhere($condition)->first();
161
162
        if(!$model) {
163
            throw new NotFoundHttpException();
164
        }
165
166
        return $this->update($model->id, $attributes);
167
    }
168
169
    /**
170
     * Delete a tenant by ID
171
     *
172
     * @param int $id
173
     *
174
     * @return mixed
175
     */
176
    public function delete($id)
177
    {
178
        if($model = $this->findById($id)) {
179
            $model->delete();
180
        }
181
    }
182
183
    /**
184
     * Force delete a tenant by ID
185
     *
186
     * @param int $id
187
     *
188
     * @return mixed
189
     */
190
    public function forceDelete($id)
191
    {
192
        if($model = $this->findById($id)) {
193
            $model->forceDelete();
194
        }
195
    }
196
}