ModelProxy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 8
dl 0
loc 25
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelAttribute() 0 3 1
A __construct() 0 4 1
A __get() 0 3 2
A getTableField() 0 3 1
1
<?php
2
3
namespace N7olkachev\ComputedProperties;
4
5
use Illuminate\Database\Query\Expression;
6
7
class ModelProxy
8
{
9
    protected $model;
10
11
    protected $runningInQuery;
12
13
    public function __construct($model, $runningInQuery)
14
    {
15
        $this->model = $model;
16
        $this->runningInQuery = $runningInQuery;
17
    }
18
19
    public function __get($key)
20
    {
21
        return $this->runningInQuery ? $this->getTableField($key) : $this->getModelAttribute($key);
22
    }
23
24
    protected function getTableField($key)
25
    {
26
        return new Expression($this->model->getTable() . '.' . $key);
27
    }
28
29
    protected function getModelAttribute($key)
30
    {
31
        return $this->model->$key;
32
    }
33
}
34