Switchable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 13
c 2
b 1
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A switchableFields() 0 4 1
A switchOn() 0 10 2
A switchOff() 0 10 2
1
<?php
2
3
namespace SoareCostin\LaravelToggleSwitchFields\Traits;
4
5
trait Switchable
6
{
7
    protected function switchableFields()
8
    {
9
        return [
10
            config('toggle_switch_fields.default_field'),
11
        ];
12
    }
13
14
    /**
15
     * Switch the current resource on.
16
     *
17
     * @param  \Illuminate\Database\Eloquent\Model  $entity
18
     * @param  string $fieldName
19
     * @return
20
     */
21
    public function switchOn($entity, $fieldName)
22
    {
23
        if (! in_array($fieldName, $this->switchableFields())) {
24
            abort(404);
25
        }
26
27
        $success = $entity->update([$fieldName => 1]);
28
29
        return response()->json([
30
            'ok' => $success,
31
        ]);
32
    }
33
34
    /**
35
     * Switch the current resource off.
36
     *
37
     * @param  \Illuminate\Database\Eloquent\Model  $entity
38
     * @param  string $fieldName
39
     * @return
40
     */
41
    public function switchOff($entity, $fieldName)
42
    {
43
        if (! in_array($fieldName, $this->switchableFields())) {
44
            abort(404);
45
        }
46
47
        $success = $entity->update([$fieldName => 0]);
48
49
        return response()->json([
50
            'ok' => $success,
51
        ]);
52
    }
53
}
54