Observer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A saving() 0 6 2
A cleanAttributes() 0 10 2
1
<?php
2
3
namespace Sofa\Eloquence\AttributeCleaner;
4
5
use Sofa\Eloquence\Contracts\CleansAttributes;
6
7
class Observer
8
{
9
    /**
10
     * Saving event handler.
11
     *
12
     * @param  mixed $model
13
     * @return void
14
     */
15
    public function saving($model)
16
    {
17
        if ($model instanceof CleansAttributes) {
18
            $this->cleanAttributes($model);
19
        }
20
    }
21
22
    /**
23
     * Get rid of attributes that are not correct columns on this model's table.
24
     *
25
     * @param  \Sofa\Eloquence\Contracts\CleansAttributes $model
26
     * @return void
27
     */
28
    protected function cleanAttributes(CleansAttributes $model)
29
    {
30
        $dirty = array_keys($model->getDirty());
31
32
        $invalidColumns = array_diff($dirty, $model->getColumnListing());
33
34
        foreach ($invalidColumns as $column) {
35
            unset($model->{$column});
36
        }
37
    }
38
}
39