SettingsQueryBuilder::first()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Thinkone\NovaPageSettings\QueryAdapter;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * @extends \Illuminate\Database\Eloquent\Builder<Model>
10
 */
11
class SettingsQueryBuilder extends EloquentBuilder
12
{
13
14 2
    public function find($id, $columns = [ '*' ]): mixed
15
    {
16 2
        return $this->get()->firstWhere(InternalSettingsModel::ATTR_ID, $id);
17
    }
18
19 2
    public function first($columns = [ '*' ]): mixed
20
    {
21
        /* ad-hoc solution to prevent real call to database */
22 2
        $wheres = $this->query->wheres;
23 2
        $key    = InternalSettingsModel::ATTR_ID;
24 2
        $val    = collect($wheres)->firstWhere('column', "{$this->query->from}.{$key}");
0 ignored issues
show
Bug introduced by
$wheres of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

24
        $val    = collect(/** @scrutinizer ignore-type */ $wheres)->firstWhere('column', "{$this->query->from}.{$key}");
Loading history...
25
26 2
        if ($val) {
27 1
            return $this->find($val['value']);
28
        }
29
30 1
        return parent::first($columns);
31
    }
32
33 1
    public function exists(): bool
34
    {
35 1
        return !!$this->first();
36
    }
37
}
38