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

ApiValidations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A doCustomPreCheck() 0 10 2
A checkApiDefined() 0 9 2
A doValidations() 0 10 1
A validateMethodType() 0 8 3
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers\ApiController;
4
5
class ApiValidations
6
{
7
    /**
8
     * @param $rowApi
9
     * @param $ctrl
10
     * @return mixed
11
     */
12
    public static function checkApiDefined($rowApi, $ctrl)
13
    {
14
        if (!is_null($rowApi)) {
15
            return true;
16
        }
17
        //todo : translation
18
        $msg = 'Sorry this API is no longer available, maybe has changed by admin, or please make sure api url is correct.';
19
        $result = ApiResponder::makeResult(0, $msg);
20
        ApiResponder::send($result, request()->all(), $ctrl);
21
    }
22
23
24
    /**
25
     * @param $methodType
26
     * @param $ctrl
27
     * @return mixed
28
     */
29
    public static function validateMethodType($methodType, $ctrl)
30
    {
31
        if (!is_null($methodType) && request()->isMethod($methodType)) {
32
            return true;
33
        }
34
        //todo : translation
35
        $result = ApiResponder::makeResult(0, "The request method is not allowed !");
36
        ApiResponder::send($result, request()->all(), $ctrl);
37
    }
38
39
    /**
40
     * @param $ctrl
41
     * @return mixed
42
     */
43
    public static function doCustomPreCheck($ctrl)
44
    {
45
        $ctrl->hookValidate();
46
47
        if (! $ctrl->validate) {
48
            return true;
49
        }  // hook have to return true
50
        //todo : translation
51
        $result = ApiResponder::makeResult(0, 'Failed to execute API !');
52
        ApiResponder::send($result, request()->all(), $ctrl);
53
    }
54
55
56
    /**
57
     * @param $rowApi
58
     * @param $ctrl
59
     */
60
    public static function doValidations($rowApi, $ctrl)
61
    {
62
        /* Check the row is exists or not */
63
        self::checkApiDefined($rowApi, $ctrl);
64
65
        /* Method Type validation */
66
        self::validateMethodType($rowApi->method_type, $ctrl);
67
68
        /* Do some custom pre-checking for posted data, if failed discard API execution */
69
        self::doCustomPreCheck($ctrl);
70
    }
71
}