Store   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 40 4
1
<?php
2
3
namespace App\Http\Controllers\Company;
4
5
use App\Jobs\Tenant\CreateDB;
6
use App\Jobs\Tenant\Migration;
7
use App\Models\Company as Company1;
8
use App\Traits\TenantConnectionResolver;
9
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10
use Illuminate\Routing\Controller;
11
use LaravelEnso\Companies\Http\Requests\ValidateCompany;
12
use LaravelEnso\Companies\Models\Company;
13
14
class Store extends Controller
15
{
16
    use AuthorizesRequests;
17
    use TenantConnectionResolver;
0 ignored issues
show
introduced by
The trait App\Traits\TenantConnectionResolver requires some properties which are not provided by App\Http\Controllers\Company\Store: $role_id, $connection
Loading history...
18
19
    public function __invoke(ValidateCompany $request, Company $company)
20
    {
21
        $role = \Auth::user()->role_id;
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
22
        $companies = \Auth::user()->person->companies()->count();
0 ignored issues
show
Bug introduced by
Accessing person on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
23
        if (in_array($role, [1, 4, 5, 6]) && $companies <= 1) {
24
            $company->fill($request->validatedExcept('mandatary'));
25
26
            // 1 = public
27
            // 2 = private
28
            $clone = $request->post();
29
            $user = \Auth::user();
30
            $user_id = $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
            $person_name = $user->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
32
            $user_email = $user->email;
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
            $this->authorize('store', $company);
34
35
            $company->privacy = $request->privacy;
36
            $company->save();
37
            if ($user->role_id !== 1) {
38
                $c = new Company1();
39
40
                $c->fill($clone);
0 ignored issues
show
Bug introduced by
It seems like $clone can also be of type null and string; however, parameter $attributes of Illuminate\Database\Eloquent\Model::fill() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

40
                $c->fill(/** @scrutinizer ignore-type */ $clone);
Loading history...
41
                $c->privacy = $request->privacy;
0 ignored issues
show
Bug Best Practice introduced by
The property privacy does not exist on App\Models\Company. Since you implemented __set, consider adding a @property annotation.
Loading history...
42
43
                $c->setAttribute('created_by', 1);
44
                $c->setAttribute('updated_by', 1);
45
                $c->save();
46
            }
47
            CreateDB::dispatch($company, $user_id);
48
//            $company = Company::find($company->id);
49
            Migration::dispatch($company, $person_name, $user_email, $user->password);
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
50
51
            return [
52
                'message' => __('The company was successfully created'),
53
                'redirect' => 'administration.companies.edit',
54
                'param' => ['company' => $company->id],
55
            ];
56
        }
57
58
        return ['error' => __('Unauthorized')];
59
    }
60
}
61