Passed
Push — master ( d63a45...8016ac )
by Iman
09:10
created

HandleDetailsAction::handle()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 12
nop 5
dl 0
loc 34
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers\ApiController;
4
5
use Crocodicstudio\Crudbooster\Modules\ModuleGenerator\ControllerGenerator\FieldDetector;
6
use Illuminate\Support\Facades\Hash;
7
8
class HandleDetailsAction
9
{
10
    /**
11
     * @param $data
12
     * @param $parameters
13
     * @param $posts
14
     * @param $responsesFields
15
     * @param $ctrl
16
     * @return array
17
     */
18
    public static function handle($data, $parameters, $posts, $responsesFields, $ctrl)
19
    {
20
        $row = $data->first();
21
22
        if (! $row) {
23
            return ApiResponder::makeResult(0, 'There is no data found !');
24
        }
25
26
        foreach ($parameters as $param) {
27
            $name = $param['name'];
28
            $type = $param['type'];
29
            $value = $posts[$name];
30
            $used = $param['used'];
31
            $required = $param['required'];
32
33
            if ($param['config'] != '' && substr($param['config'], 0, 1) != '*') {
34
                $value = $param['config'];
35
            }
36
            if (Hash::check($value, $row->{$name})) {
37
                continue;
38
            }
39
40
            if ($required && $type == 'password') {
41
                self::passwordError($posts, $ctrl);
42
            }
43
44
            if (! $required && $used && $value) {
45
                self::passwordError($posts, $ctrl);
46
            }
47
        }
48
49
        self::handleFile($row, $responsesFields);
50
51
        return self::success($row);
52
    }
53
54
    /**
55
     * @param $posts
56
     * @return mixed
57
     */
58
    private static function passwordError($posts, $ctrl)
59
    {
60
        $result = ApiResponder::makeResult(0, cbTrans('alert_password_wrong'));
61
62
        ApiResponder::send($result, $posts, $ctrl);
63
    }
64
65
    /**
66
     * @param $rows
67
     * @return array
68
     */
69
    private static function success($rows)
70
    {
71
        $result = ApiResponder::makeResult(1, 'success');
72
73
        return array_merge($result, (array)$rows);
74
    }
75
76
    /**
77
     * @param $rows
78
     * @param $responsesFields
79
     */
80
    public static function handleFile($rows, $responsesFields)
81
    {
82
        foreach ($rows as $k => $v) {
83
            if (FieldDetector::isUploadField(\File::extension($v))) {
84
                $rows->$k = asset($v);
85
            }
86
87
            if (! in_array($k, $responsesFields)) {
88
                unset($rows->$k);
89
            }
90
        }
91
    }
92
93
}