ChartController::destroy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Dashboard;
4
5
use App\Traits\ConnectionTrait;
6
use Carbon\Carbon;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Support\Facades\Auth;
10
use LaravelEnso\Charts\Factories\Bar;
11
use LaravelEnso\Charts\Factories\Bubble;
12
use LaravelEnso\Charts\Factories\Doughnut;
13
use LaravelEnso\Charts\Factories\Line;
14
use LaravelEnso\Charts\Factories\Pie;
15
use LaravelEnso\Charts\Factories\Polar;
16
use LaravelEnso\Charts\Factories\Radar;
17
18
//use LaravelEnso\Multitenancy\Enums\Connections;
19
20
class ChartController extends Controller
21
{
22
    use ConnectionTrait;
23
    // use SystemConnection;
24
25
    public function line()
26
    {
27
        return (new Line())
28
            ->title('Income')
29
            ->labels(['January', 'February', 'March', 'April', 'May', 'June', 'July'])
30
            ->datasets([
31
                'Sales' => [65, 59, 80, 81, 26, 25, 10],
32
                'Revenue' => [15, 29, 60, 31, 56, 65, 44],
33
            ])->fill()
34
            ->get();
35
    }
36
37
    public function bar()
38
    {
39
        return (new Bar())
40
            ->title('Sales')
41
            ->labels(['Ian', 'Feb', 'Mar'])
42
            ->datasets([
43
                'Sales' => [1233, 1231, 3123],
44
                'Spendings' => [1250, 1730, 5300],
45
                'Profit' => [1250 - 1233, 1730 - 1231, 5300 - 3123],
46
            ])->get();
47
    }
48
49
    public function pie()
50
    {
51
        // \DB::table('some')->get();
52
        $male = \DB::table('people')->where('sex', 'M')->get()->count();
53
        $female = \DB::table('people')->where('sex', 'F')->get()->count();
54
        $unknown = \DB::table('people')->whereNull('sex')->get()->count();
55
        \Session::get('db', env('DB_DATABASE'));
56
        $user = Auth::user();
57
        $user->person->company();
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...
58
        \Session::get('companyId');
59
60
        // return $current_db;
61
        // return $sv;
62
        return (new Pie())
63
            ->title('Genders')
64
            ->labels(['Male', 'Female', 'Unknown'])
65
            ->datasets([$male, $female, $unknown])
66
            ->get();
67
    }
68
69
    public function doughnut()
70
    {
71
        return (new Doughnut())
72
            ->title('Colors Two')
73
            ->labels(['Green', 'Red', 'Azzure'])
74
            ->datasets([400, 50, 100])
75
            ->get();
76
    }
77
78
    public function radar()
79
    {
80
        return (new Radar())
81
            ->title('Habits')
82
            ->labels([
83
                'Eating', 'Drinking', 'Sleeping', 'Designing', 'Coding', 'Cycling', 'Running',
84
            ])
85
            ->datasets([
86
                '2005' => [65, 59, 90, 81, 56, 55, 40],
87
                '2006' => [28, 48, 40, 19, 96, 27, 100],
88
            ])->get();
89
    }
90
91
    public function polar()
92
    {
93
        return (new Polar())
94
            ->title('Again Colors')
95
            ->labels(['Green', 'Red', 'Azzure', 'Portocaliu', 'Purple'])
96
            ->datasets([11, 16, 7, 14, 14])
97
            ->get();
98
    }
99
100
    public function bubble()
101
    {
102
        return (new Bubble())
103
            ->title('City Population by Age')
104
            ->labels(['Geneva', 'Besel', 'Bucharest'])
105
            ->datasets([
106
                0 => [[1010, 59, 4800], [2011, 55, 1800], [1012, 45, 2000], [413, 58, 4400]],
107
                1 => [[2010, 48, 1700], [1211, 67, 1200], [2012, 96, 1233], [813, 35, 3000]],
108
                2 => [[1510, 44, 2000], [811, 62, 1500], [212, 55, 1299], [1213, 39, 4000]],
109
            ])->get();
110
    }
111
112
    // change database to use
113
    public function changedb(Request $request)
114
    {
115
        $prevConn = $this->getConnection();
116
        $company_id = $request->get('company_id');
117
//        $tree_id = $request->get('tree_id');
118
        if (! empty($company_id)) {
119
            $tenants = \App\Models\Tenant::find($company_id);
120
            if ($tenants) {
121
                $db = $tenants->tenancy_db_name;
0 ignored issues
show
Bug Best Practice introduced by
The property tenancy_db_name does not exist on App\Models\Tenant. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The property tenancy_db_name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
122
                $key = 'database.connections.tenant.database';
123
                config([$key => $db]);
124
                \DB::reconnect('tenantdb');
125
//                \Session::put('conn', $conn);php
126
                \Session::put('db', $db);
127
//            $db = Connections::Tenant.$company_id.'_'.$tree_id;
128
//                $this->setConnection($key, $db);
129
            }
130
        }
131
        //            $db = 'enso';
132
//            $key = 'database.default';
133
//            $this->setConnection('mysql');
134
135
        $changeConn = $this->getConnection();
136
137
        $peoplesattached = \DB::connection($changeConn)->table('people')->get()->count();
138
        $familiesjoined = \DB::connection($changeConn)->table('families')->get()->count();
139
140
        return json_encode([
141
            'db' => config('database.connections.tenant.database'),
142
            'connection' => $changeConn,
143
            'changedb' => $prevConn === $changeConn,
144
            'familiesjoined' => $familiesjoined,
145
            'peoplesattached' => $peoplesattached,
146
        ], JSON_THROW_ON_ERROR);
147
    }
148
149
    // get companies of user.
150
    public function getDB()
151
    {
152
        $user = Auth::user();
153
        $companies = $user->person->companies()->get();
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...
154
        $ret = [];
155
        $ret['company'] = $companies;
156
157
        return $ret;
158
    }
159
160
    /**
161
     * Remove the specified resource from storage.
162
     *
163
     * @param  int  $id
164
     * @return \Illuminate\Http\Response
165
     */
166
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

166
    public function destroy(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
    }
169
170
    public function trial()
171
    {
172
        $user = auth()->user();
173
        if ($user->subscribed('default')) {
174
            $days = Carbon::now()->diffInDays(Carbon::parse($user->subscription('default')->asStripeSubscription()->current_period_end));
175
        } else {
176
            $days = Carbon::now()->diffInDays($user->trial_ends_at);
177
        }
178
179
        return ['days' => $days];
180
    }
181
182
    public function changeCompany(Request $request)
183
    {
184
        $prevConn = $this->getConnection();
185
        $company_id = $request->get('company_id');
186
        $tree_id = $request->get('tree_id');
187
        if (! empty($company_id)) {
188
            $db = Connections::Tenant.$company_id.'_'.$tree_id;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Dashboard\Connections was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
189
            $this->setConnection(Connections::Tenant, $db);
190
        } else {
191
            $this->setConnection('mysql');
192
        }
193
        $changeConn = $this->getConnection();
194
195
        $peoplesattached = \DB::connection($changeConn)->table('people')->get()->count();
196
        $familiesjoined = \DB::connection($changeConn)->table('families')->get()->count();
197
198
        return json_encode([
199
            'db' => config('database.connections.tenant.database'),
200
            'connection' => $changeConn,
201
            'changedb' => $prevConn === $changeConn,
202
            'familiesjoined' => $familiesjoined,
203
            'peoplesattached' => $peoplesattached,
204
        ], JSON_THROW_ON_ERROR);
205
    }
206
}
207