Passed
Push — master ( 36df2b...89c2b4 )
by noitran
03:13
created

SqlRepository::findByField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Noitran\Repositories\Repositories;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * Class SqlRepository
9
 */
10
abstract class SqlRepository extends AbstractRepository
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 3
    public function all($columns = ['*'])
16
    {
17 3
        return parent::all(
18 3
            $this->getColumnNames($columns)
19
        );
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25 1
    public function paginate(int $perPage = null, $columns = ['*'])
26
    {
27 1
        return parent::paginate(
28 1
            $perPage,
29 1
            $this->getColumnNames($columns)
30
        );
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36 1
    public function simplePaginate(int $perPage = null, $columns = ['*'])
37
    {
38 1
        return parent::simplePaginate(
39 1
            $perPage,
40 1
            $this->getColumnNames($columns)
41
        );
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 3
    public function find($id, $columns = ['*'])
48
    {
49 3
        return parent::find(
50 3
            $id,
51 3
            $this->getColumnNames($columns)
52
        );
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function findByField($field, $value = null, $columns = ['*'])
59
    {
60
        return parent::findByField(
61
            $this->getColumnName($field),
62
            $value,
63
            $this->getColumnNames($columns)
64
        );
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 1
    public function first($columns = ['*']): ?Model
71
    {
72 1
        return parent::first(
73 1
            $this->getColumnNames($columns)
74
        );
75
    }
76
77
    /**
78
     * @param string $column
79
     * @param mixed $model
80
     *
81
     * @return string
82
     */
83 9
    public function getColumnName($column, $model = null): string
84
    {
85 9
        $model = $model ?? $this->model;
86
87 9
        return ! strpos($column, '.')
88 9
            ? $this->getSchemaName($model) . '.' . $column
89 9
            : $column;
90
    }
91
}
92