Completed
Push — master ( a8a3b2...e20e99 )
by Ricardo
02:07 queued 12s
created

TokenController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 261
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermissionOptions() 0 4 1
A __construct() 0 29 1
1
<?php
2
3
namespace Integrations\Http\Controllers\RiCa\WebServicos;
4
5
use Illuminate\Http\Request;
6
use Integrations\Exceptions\Exception;
7
use Integrations\Http\Controllers\Controller as Base;
8
use Integrations\Models\Token;
9
// use Pedreiro\Http\Controllers\Admin\Base;
10
use Yajra\Datatables\Datatables;
11
12
class TokenController extends Base
13
{
14
    /**
15
     * @var string
16
     */
17
    public $title = 'Tokens';
18
    public $model = Token::class;
19
20
    /**
21
     * @var string
22
     */
23
    public $description = 'Listagem de Tokens.';
24
25
    /**
26
     * @var array
27
     */
28
    public $columns = [
29
        'Rule' => 'getAdminTitleAttribute',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    public $search = [
36
        'from',
37
        'to',
38
        'code' => [
39
            'type' => 'select',
40
            'options' => 'Facilitador\Models\RedirectRule::getCodes()',
41
        ],
42
        'label',
43
    ];
44
45
    /**
46
     * Get the permission options.
47
     *
48
     * @return array An associative array.
49
     */
50
    public function getPermissionOptions()
51
    {
52
        return array_except(parent::getPermissionOptions(), ['publish']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Integrations\Http\Controllers\Controller as the method getPermissionOptions() does only exist in the following sub-classes of Integrations\Http\Controllers\Controller: Integrations\Http\Contro...ervicos\TokenController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
53
    }
54
55
    /**
56
     * Populate protected properties on init
57
     */
58
    public function __construct()
59
    {
60
        $this->title = __('pedreiro::redirect_rules.controller.title');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('pedreiro::redirect_rules.controller.title') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $title is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
61
        $this->description = __('pedreiro::redirect_rules.controller.description');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('pedreiro::redirect_r...ontroller.description') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $description is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
62
        $this->columns = [
63
            __('pedreiro::redirect_rules.controller.column.rule') => 'getAdminTitleAttribute',
64
        ];
65
        $this->search = [
66
            'from' => [
67
                'label' => __('pedreiro::redirect_rules.controller.search.from'),
68
                'type' => 'text',
69
            ],
70
            'to' => [
71
                'label' => __('pedreiro::redirect_rules.controller.search.to'),
72
                'type' => 'text',
73
            ],
74
            'code' => [
75
                'label' => __('pedreiro::redirect_rules.controller.search.code'),
76
                'type' => 'select',
77
                'options' => 'Facilitador\Models\RedirectRule::getCodes()',
78
            ],
79
            'label' => [
80
                'label' => __('pedreiro::redirect_rules.controller.search.label'),
81
                'type' => 'text',
82
            ],
83
        ];
84
85
        // parent::__construct();
86
    }
87
88
    // /**
89
    //  * @var string
90
    //  */
91
    // public $description = "Listagem de Pedidos.";
92
93
    // /**
94
    //  * Display all the workers
95
    //  *
96
    //  * @return Illuminate\View\View
97
    //  */
98
    // public function index(Request $request)
99
    // {
100
    //     return $this->populateView(
101
    //         'admin.orders.index', [
102
    //         'orders' => Order::orderBy('id', 'DESC')->simplePaginate(50),
103
    //         ]
104
    //     );
105
    // }
106
107
    // /**
108
    //  * Ajax service that tails the log file for the selected worker
109
    //  *
110
    //  * @param $worker
111
    //  */
112
    // public function tail($worker)
113
    // {
114
    //     // Form the path to the file
115
    //     $file = Worker::logPath(urldecode($worker));
116
    //     if (!file_exists($file)) {
117
    //         throw new Exception('Log not found: '.$file);
118
    //     }
119
    //     $size = 1024 * 100; // in bytes to get
120
121
    //     // Read from the end of the file
122
    //     clearstatcache();
123
    //     $fp = fopen($file, 'r');
124
    //     fseek($fp, -$size, SEEK_END);
125
    //     $contents = explode("\n", fread($fp, $size));
126
    //     fclose($fp);
127
128
    //     // Reverse the contents and return
129
    //     $contents = array_reverse($contents);
130
    //     if (empty($contents[0])) {
131
    //         array_shift($contents);
132
    //     }
133
    //     die(implode("\n", $contents));
134
    // }
135
136
    // /**
137
    //  * Display a listing of the resource.
138
    //  *
139
    //  * @return \Illuminate\Http\Response
140
    //  */
141
    // public function index(Request $request)
142
    // {
143
    //     // if ($request->ajax()) {
144
    //     //     $query = Order::with('user', 'operadora', 'collaborator', 'customer', 'money')->select('orders.*');
145
146
    //     //     return Datatables::of($query)->addColumn('action', function ($order) {
147
    //     //         return '<a href="'.route('admin.orders.show',$order->id).'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-eye"></i> Show</a>';
148
    //     //     })->setRowId('id')->editColumn('created_at', function ($order) {
149
    //     //         return $order->created_at->format('h:m:s d/m/Y');
150
    //     //     })
151
    //     //     ->setRowClass(function ($order) {
152
    //     //         return $order->status == Order::$STATUS_APPROVED ? 'alert-success' : 'alert-warning';
153
    //     //     })
154
    //     //     ->setRowData([
155
    //     //         'id' => 'test',
156
    //     //     ])
157
    //     //     ->setRowAttr([
158
    //     //         'color' => 'red',
159
    //     //     ])->make(true);
160
161
    //     //     return $this->dataTable->eloquent($query)->make(true);
162
    //     // }
163
    //     // return view('admin.orders.index');
164
165
    //     // USando Service //public function index(UsersDataTable $dataTable)
166
    //     // return $dataTable->render('orders.index');
167
168
    //     if ($request->has('query') && !empty($request->input('query'))) {
169
    //         dd('oi');
170
    //         $orders = Order::search($request->input('query'))->orderBy('id', 'DESC')->simplePaginate(50);
171
    //     } else {
172
    //         $orders = Order::orderBy('id', 'DESC')->simplePaginate(50);
173
    //     }
174
    //     return view('admin.orders.index', compact('orders'));
175
    // }
176
177
    // /**
178
    //  * Show the form for creating a new resource.
179
    //  *
180
    //  * @return \Illuminate\Http\Response
181
    //  */
182
    // public function create()
183
    // {
184
    //     return view('admin.orders.create');
185
    // }
186
187
    // /**
188
    //  * Store a newly created resource in storage.
189
    //  *
190
    //  * @param  \Illuminate\Http\Request  $request
191
    //  * @return \Illuminate\Http\Response
192
    //  */
193
    // public function store(Request $request)
194
    // {
195
    //   $request->validate([
196
    //     'total'=>'required',
197
    //     'money'=> 'required|integer',
198
    //     'operadora' => 'required|integer'
199
    //   ]);
200
    //   $order = new Order([
201
    //     'total' => $request->get('total'),
202
    //     'money'=> $request->get('money'),
203
    //     'operadora'=> $request->get('operadora')
204
    //   ]);
205
    //   $order->save();
206
    //   return redirect('/orders')->with('success', 'Stock has been added');
207
    // }
208
209
    // /**
210
    //  * Display the specified resource.
211
    //  *
212
    //  * @param  int  $id
213
    //  * @return \Illuminate\Http\Response
214
    //  */
215
    // public function show(Request $request, $id)
216
    // {
217
    //     $order = Order::findOrFail($id);
218
    //     return view('admin.orders.show', compact('order'));
219
    // }
220
    
221
222
    // /**
223
    //  * Show the form for editing the specified resource.
224
    //  *
225
    //  * @param  int  $id
226
    //  * @return \Illuminate\Http\Response
227
    //  */
228
    // public function edit(Request $request, $id)
229
    // {
230
    //     $order = Order::find($id);
231
232
    //     return view('admin.orders.edit', compact('order'));
233
    // }
234
235
    // /**
236
    //  * Update the specified resource in storage.
237
    //  *
238
    //  * @param  \Illuminate\Http\Request  $request
239
    //  * @param  int  $id
240
    //  * @return \Illuminate\Http\Response
241
    //  */
242
    // public function update(Request $request, $id)
243
    // {
244
    //     $request->validate([
245
    //         'total'=>'required',
246
    //         'money'=> 'required|integer',
247
    //         'operadora' => 'required|integer'
248
    //     ]);
249
250
    //     $order = Order::findOrFail($id);
251
    //     $order->total = $request->get('total');
252
    //     $order->money = $request->get('money');
253
    //     $order->operadora = $request->get('operadora');
254
    //     $order->save();
255
256
    //     return redirect('/orders')->with('success', 'Stock has been updated');
257
    // }
258
259
    // /**
260
    //  * Remove the specified resource from storage.
261
    //  *
262
    //  * @param  int  $id
263
    //  * @return \Illuminate\Http\Response
264
    //  */
265
    // public function destroy(Request $request, $id)
266
    // {
267
    //     $order = Order::findOrFail($id);
268
    //     $order->delete();
269
270
    //     return redirect('/orders')->with('success', 'Stock has been deleted Successfully');
271
    // }
272
}
273