BudgetAbsorptionController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
<?php namespace Bantenprov\BudgetAbsorption\Http\Controllers;
2
3
/* require */
4
use App\Http\Controllers\Controller;
5
use Illuminate\Http\Request;
6
use Bantenprov\BudgetAbsorption\Facades\BudgetAbsorption;
7
8
/* Models */
9
use Bantenprov\BudgetAbsorption\Models\Bantenprov\BudgetAbsorption\BudgetAbsorption as PdrbModel;
10
use Bantenprov\BudgetAbsorption\Models\Bantenprov\BudgetAbsorption\Province;
11
use Bantenprov\BudgetAbsorption\Models\Bantenprov\BudgetAbsorption\Regency;
12
13
/* etc */
14
use Validator;
15
16
/**
17
 * The BudgetAbsorptionController class.
18
 *
19
 * @package Bantenprov\BudgetAbsorption
20
 * @author  bantenprov <[email protected]>
21
 */
22
class BudgetAbsorptionController extends Controller
23
{
24
25
    protected $province;
26
27
    protected $regency;
28
29
    protected $budget_absorption;
30
31
    public function __construct(Regency $regency, Province $province, PdrbModel $budget_absorption)
32
    {
33
        $this->regency  = $regency;
34
        $this->province = $province;
35
        $this->budget_absorption     = $budget_absorption;
36
    }
37
38
    public function index(Request $request)
39
    {
40
        /* todo : return json */
41
42
        return 'index';
43
44
    }
45
46
    public function create()
47
    {
48
49
        return response()->json([
50
            'provinces' => $this->province->all(),
51
            'regencies' => $this->regency->all()
52
        ]);
53
    }
54
55
    public function show($id)
56
    {
57
58
        $budget_absorption = $this->budget_absorption->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Bantenprov\Budget...ption\BudgetAbsorption>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
59
60
        return response()->json([
61
            'negara'    => $budget_absorption->negara,
62
            'province'  => $budget_absorption->getProvince->name,
63
            'regencies' => $budget_absorption->getRegency->name,
64
            'tahun'     => $budget_absorption->tahun,
65
            'data'      => $budget_absorption->data
66
        ]);
67
    }
68
69
    public function store(Request $request)
70
    {
71
72
        $validator = Validator::make($request->all(),[
73
            'negara'        => 'required',
74
            'province_id'   => 'required',
75
            'regency_id'    => 'required',
76
            'kab_kota'      => 'required',
77
            'tahun'         => 'required|integer',
78
            'data'          => 'required|integer',
79
        ]);
80
81
        if($validator->fails())
82
        {
83
            return response()->json([
84
                'title'     => 'error',
85
                'message'   => 'add failed',
86
                'type'      => 'failed',
87
                'errors'    => $validator->errors()
88
            ]);
89
        }
90
91
        $check = $this->budget_absorption->where('regency_id',$request->regency_id)->where('tahun',$request->tahun)->count();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Bantenprov\Budget...ption\BudgetAbsorption>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
93
        if($check > 0)
94
        {
95
            return response()->json([
96
                'title'         => 'error',
97
                'message'       => 'Data allready exist',
98
                'type'          => 'failed',
99
            ]);
100
101
        }else{
102
            $data = $this->budget_absorption->create($request->all());
0 ignored issues
show
Bug introduced by
The method create() does not exist on Bantenprov\BudgetAbsorpt...rption\BudgetAbsorption. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
103
104
            return response()->json([
105
                    'type'      => 'success',
106
                    'title'     => 'success',
107
                    'id'      => $data->id,
108
                    'message'   => 'PDRB '. $this->regency->find($request->regency_id)->name .' tahun '. $request->tahun .' successfully created',
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Bantenprov\Budget...dgetAbsorption\Regency>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109
                ]);
110
        }
111
112
    }
113
114
    public function update(Request $request, $id)
115
    {
116
        /* todo : return json */
117
        return '';
118
119
    }
120
121
    public function destroy($id)
122
    {
123
        /* todo : return json */
124
        return '';
125
126
    }
127
}
128
129