ModelController::revert()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace Tracking\Http\Controllers\Metrics;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use Tracking\Providers\Metrics\LogParser;
8
use Tracking\Models\Metrics\LarametricsModel;
9
use DB;
10
use Carbon\Carbon;
11
12
class ModelController extends Controller
13
{
14
    
15
    public function index()
16
    {
17
        $modelChanges = LarametricsModel::groupBy('model')
18
            ->select('model', DB::raw('count(*) as total'))
19
            ->get()
20
            ->keyBy('model');
21
            
22
        $modelsAmounts = array();
23
24
        $earliestModel = LarametricsModel::orderBy('created_at', 'desc')
25
            ->first();
26
27
        foreach(\Illuminate\Support\Facades\Config::get('larametrics.modelsWatched') as $model) {
28
            $modelsAmounts[$model] = array(
29
                'count' => $model::count(),
30
                'changes' => isset($modelChanges[$model]) ? $modelChanges[$model]['total'] : 0
31
            );
32
        }
33
34
        return view(
35
            'rica.larametrics::models.index', [
36
            'modelsAmounts' => $modelsAmounts,
37
            'pageTitle' => 'Database Models',
38
            'pageSubtitle' => 'Data shown is only by models being watched by Larametrics',
39
            'watchLength' => $earliestModel ? $earliestModel->created_at->diffInDays(Carbon::now()) : 0
40
            ]
41
        );
42
    }
43
44
    public function show($model)
45
    {
46
        if(is_numeric($model)) {
47
            $larametricsModel = LarametricsModel::find($model);
48
49
            $modelPrimaryKey = (new $larametricsModel->model)->getKeyName();
50
51
            return view(
52
                'rica.larametrics::models.show', [
53
                'model' => $larametricsModel,
54
                'pageTitle' => $larametricsModel->model,
55
                'modelPrimaryKey' => $modelPrimaryKey
56
                ]
57
            );
58
        } else {
59
            $appModel = str_replace('+', '\\', $model);
60
            
61
            $models = LarametricsModel::where('model', $appModel)
62
                ->orderBy('created_at', 'desc')
63
                ->get();
64
65
            $earliestModel = LarametricsModel::orderBy('created_at', 'desc')
66
                ->first();
67
68
            $modelPrimaryKey = (new $appModel)->getKeyName();
69
70
            return view(
71
                'rica.larametrics::models.model', [
72
                'models' => $models,
73
                'pageTitle' => $appModel,
74
                'watchLength' => $earliestModel ? $earliestModel->created_at->diffInDays(Carbon::now()) : 0,
75
                'modelPrimaryKey' => $modelPrimaryKey
76
                ]
77
            );
78
        }
79
    }
80
81
    public function revert(LarametricsModel $model)
82
    {
83
        $original = json_decode($model->original, true);
0 ignored issues
show
Documentation introduced by
The property $original is declared protected in Illuminate\Database\Eloq...\Concerns\HasAttributes. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
        $revertModel = $model->model::find($original['id']);
85
86
        if($model->method === 'created') {
87
88
            if($revertModel) {
89
                $revertModel->delete();
90
            }
91
92
        } elseif($model->method === 'deleted') {
93
94
            unset($original['id']);
95
            $model->model::create($original);
96
97
        } else {
98
99
            if($revertModel) {
100
                unset($original['updated_at']);
101
                $revertModel->update($original);
102
            }
103
        
104
        }
105
106
        return redirect()->route('rica.larametrics::models.index');
107
    }
108
109
}
110