CalculatorApiController::dataVerify()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 7.756
cc 9
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Transformers\CalculatorTransformer;
6
use App\SimulatorHistory;
7
use App\User;
8
use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;
9
use DB;
10
use Illuminate\Http\Request;
11
use App\Http\Requests;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Input;
14
use Vinkla\Alert\Alert;
15
16
/**
17
 * Class CalculatorApiController
18
 * @package App\Http\Controllers
19
 */
20
class CalculatorApiController extends ApiGuardController
21
{
22
23
    /**
24
     * @var CalculatorTransformer
25
     */
26
    protected $calcul_transformer;
27
28
    /**
29
     * @var SimulatorHistory
30
     */
31
    protected $calculator;
32
33
    protected $alert;
34
    /**
35
     * @var array
36
     */
37
    protected $apiMethods = [
38
        'store' => [
39
            'keyAuthentication' => true
40
        ],
41
        'getUserCalculs' => [
42
            'keyAuthentication' => true
43
        ]
44
    ];
45
46
    /**
47
     * CalculatorController constructor.
48
     * @param CalculatorTransformer $calcul_transformer
49
     * @param User $user
50
     * @param SimulatorHistory $calculator
51
     */
52
    public function __construct(Alert $alert,CalculatorTransformer $calcul_transformer, User $user, SimulatorHistory $calculator)
53
    {
54
        parent::__construct();
55
56
57
        $this->calcul_transformer = $calcul_transformer;
58
        $this->user = $user;
59
        $this->calculator = $calculator;
60
        $this->alert=$alert;
61
    }
62
63
    /**
64
     *Store calculator data in DB
65
     * @param Request $request
66
     * @return mixed
67
     */
68
    public function store(Request $request)
69
    {
70
        $user = Auth::user();
71
72
73
        if ($this->dataVerify($request)) {
74
            $calcul = array(
75
                'user_id' => $user->id,
76
                'name' => $request->input('name'),
77
                'quantity_to_buy' => $request->input('quantity_to_buy'),
78
                'quote_to_buy' => $request->input('quote_to_buy'),
79
                'price_to_buy' => $request->input('price_to_buy'),
80
                'quantity_to_sell' => $request->input('quantity_to_sell'),
81
                'quote_to_sell' => $request->input('quote_to_sell'),
82
                'tax_percent_to_discount' => $request->input('tax_percent_to_discount'),
83
                'price_to_sell' => $request->input('price_to_sell'),
84
                'gains_or_losses' => $request->input('gains_or_losses'),
85
            );
86
87
88
            $this->alert->success('El calcul ha estat guardat');
89
90
91
            $this->calculator->create($calcul);
92
93
            return $this->response->withItem($calcul, $this->calcul_transformer);
0 ignored issues
show
Documentation introduced by
$this->calcul_transformer is of type object<App\Http\Transfor...\CalculatorTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
        }
95
96
        return "Error desconegut";
97
    }
98
99
    /**
100
     * @return array|static[]
101
     */
102 View Code Duplication
    public function getUserCalculs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $user = Auth::user();
105
106
        $data = DB::table('simulator_history')->where('user_id', '=', $user->id)->get();
107
108
        return $data;
109
    }
110
111
    public function dataVerify(Request $request)
112
    {
113
        if (is_numeric($request->input('quantity_to_buy')) && is_numeric($request->input('quote_to_buy')) && is_numeric($request->input('price_to_buy')) &&
114
            is_numeric($request->input('quantity_to_sell')) && is_numeric($request->input('quote_to_sell')) && is_numeric($request->input('tax_percent_to_discount')) &&
115
            is_numeric($request->input('price_to_sell')) && is_numeric($request->input('gains_or_losses'))
116
        ) {
117
            return true;
118
        } else {
119
            dd("Les dades son errones");
120
            return false;
121
        }
122
    }
123
}
124