Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

Cacheable::filterFromColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 11.10.18
6
 * Time: 23:33.
7
 */
8
9
namespace Foundation\Traits;
10
11
12
use Foundation\Cache\ModelCache;
13
14
trait Cacheable
15
{
16
    public static function find($id, $columns = ['*'])
17
    {
18
        $model = ModelCache::findOrRequery($id, get_called_class());
19
        return self::filterFromColumns($model, $columns);
20
    }
21
22
    public static function findWithoutCache($id, $columns = ['*'])
23
    {
24
        return get_parent_class(self::class)::find($id, $columns);
25
    }
26
27
    private static function filterFromColumns($model, $columns)
28
    {
29
        if ($columns !== ['*']) {
30
            return collect($model)->first($columns);
31
        }
32
        return $model;
33
    }
34
}
35