Passed
Push — master ( 70d4c6...21d85a )
by Iman
05:00
created

HandleDetailsAction::handle()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 12
nop 5
dl 0
loc 34
rs 5.2653
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 Illuminate\Support\Facades\Hash;
6
7
class HandleDetailsAction
8
{
9
    /**
10
     * @param $data
11
     * @param $parameters
12
     * @param $posts
13
     * @param $responsesFields
14
     * @param $ctrl
15
     * @return array
16
     */
17
    public static function handle($data, $parameters, $posts, $responsesFields, $ctrl)
18
    {
19
        $row = $data->first();
20
21
        if (! $row) {
22
            return ApiResponder::makeResult(0, 'There is no data found !');
23
        }
24
25
        foreach ($parameters as $param) {
26
            $name = $param['name'];
27
            $type = $param['type'];
28
            $value = $posts[$name];
29
            $used = $param['used'];
30
            $required = $param['required'];
31
32
            if ($param['config'] != '' && substr($param['config'], 0, 1) != '*') {
33
                $value = $param['config'];
34
            }
35
            if (Hash::check($value, $row->{$name})) {
36
                continue;
37
            }
38
39
            if ($required && $type == 'password') {
40
                self::passwordError($posts, $ctrl);
41
            }
42
43
            if (! $required && $used && $value) {
44
                self::passwordError($posts, $ctrl);
45
            }
46
        }
47
48
        self::handleFile($row, $responsesFields);
0 ignored issues
show
Bug Best Practice introduced by
The method crocodicstudio\crudboost...ilsAction::handleFile() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        self::/** @scrutinizer ignore-call */ 
49
              handleFile($row, $responsesFields);
Loading history...
49
50
        return self::success($row);
51
    }
52
53
    /**
54
     * @param $posts
55
     * @return mixed
56
     */
57
    private static function passwordError($posts, $ctrl)
58
    {
59
        $result = ApiResponder::makeResult(0, cbTrans('alert_password_wrong'));
60
61
        ApiResponder::send($result, $posts, $ctrl);
62
    }
63
64
    /**
65
     * @param $rows
66
     * @return array
67
     */
68
    private static function success($rows)
69
    {
70
        $result = ApiResponder::makeResult(1, 'success');
71
72
        return array_merge($result, (array)$rows);
73
    }
74
75
    /**
76
     * @param $rows
77
     * @param $responsesFields
78
     */
79
    private function handleFile($rows, $responsesFields)
80
    {
81
        foreach ($rows as $k => $v) {
82
            if (FieldDetector::isUploadField(\File::extension($v))) {
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudboost...ontroller\FieldDetector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
                $rows->$k = asset($v);
84
            }
85
86
            if (! in_array($k, $responsesFields)) {
87
                unset($rows->$k);
88
            }
89
        }
90
    }
91
92
}