SettingsQueryBuilder::exists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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