CreateStatisticsTables   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 182
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 28
loc 182
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 28 136 1
A down() 0 18 1
A jsonable() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Facades\Schema;
6
use Illuminate\Database\Schema\Blueprint;
7
use Illuminate\Database\Migrations\Migration;
8
9
class CreateStatisticsTables extends Migration
10
{
11
    /**
12
     * Run the migrations.
13
     *
14
     * @return void
15
     */
16
    public function up(): void
17
    {
18
        Schema::create(
19
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.data'), function (Blueprint $table) {
20
                // Columns
21
                $table->increments('id');
22
                $table->string('session_id');
23
                $table->nullableMorphs('user');
24
                $table->integer('status_code');
25
                $table->text('uri');
26
                $table->string('method');
27
                $table->{$this->jsonable()}('server');
28
                $table->{$this->jsonable()}('input')->nullable();
29
                $table->timestamp('created_at')->nullable();
30
            }
31
        );
32
        Schema::create(
33 View Code Duplication
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.routes'), function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
                // Columns
35
                $table->increments('id');
36
                $table->string('name');
37
                $table->string('path');
38
                $table->string('action');
39
                $table->string('middleware')->nullable();
40
                $table->{$this->jsonable()}('parameters')->nullable();
41
                $table->integer('count')->unsigned()->default(0);
42
43
                // Indexes
44
                $table->unique('name');
45
            }
46
        );
47
        Schema::create(
48
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.paths'), function (Blueprint $table) {
49
                // Columns
50
                $table->increments('id');
51
                $table->string('host');
52
                $table->string('locale');
53
                $table->string('path');
54
                $table->string('method');
55
                $table->{$this->jsonable()}('parameters')->nullable();
56
                $table->integer('count')->unsigned()->default(0);
57
58
                // Indexes
59
                $table->unique(['host', 'path', 'method', 'locale']);
60
            }
61
        );
62
        Schema::create(
63
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.agents'), function (Blueprint $table) {
64
                // Columns
65
                $table->increments('id');
66
                $table->string('name');
67
                $table->string('kind');
68
                $table->string('family');
69
                $table->string('version')->nullable();
70
                $table->integer('count')->unsigned()->default(0);
71
            }
72
        );
73
        Schema::create(
74 View Code Duplication
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.devices'), function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
                // Columns
76
                $table->increments('id');
77
                $table->string('family');
78
                $table->string('model')->nullable();
79
                $table->string('brand')->nullable();
80
                $table->integer('count')->unsigned()->default(0);
81
            }
82
        );
83
        Schema::create(
84 View Code Duplication
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.platforms'), function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
                // Columns
86
                $table->increments('id');
87
                $table->string('family');
88
                $table->string('version')->nullable();
89
                $table->integer('count')->unsigned()->default(0);
90
            }
91
        );
92
        Schema::create(
93
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.geoips'), function (Blueprint $table) {
94
                // Columns
95
                $table->increments('id');
96
                $table->string('client_ip');
97
                $table->string('latitude');
98
                $table->string('longitude');
99
                $table->char('country_code', 2);
100
                $table->{$this->jsonable()}('client_ips')->nullable();
101
                $table->boolean('is_from_trusted_proxy')->default(0);
102
                $table->string('division_code')->nullable();
103
                $table->string('postal_code')->nullable();
104
                $table->string('timezone')->nullable();
105
                $table->string('city')->nullable();
106
                $table->integer('count')->unsigned()->default(0);
107
108
                // Indexes
109
                $table->unique(['client_ip', 'latitude', 'longitude']);
110
            }
111
        );
112
        Schema::create(
113
            \Illuminate\Support\Facades\Config::get('tracking.statistics.tables.requests'), function (Blueprint $table) {
114
                // Columns
115
                $table->increments('id');
116
                $table->integer('route_id')->unsigned();
117
                $table->integer('agent_id')->unsigned();
118
                $table->integer('device_id')->unsigned();
119
                $table->integer('platform_id')->unsigned();
120
                $table->integer('path_id')->unsigned();
121
                $table->integer('geoip_id')->unsigned();
122
                $table->nullableMorphs('user');
123
                $table->string('session_id');
124
                $table->integer('status_code');
125
                $table->string('protocol_version')->nullable();
126
                $table->text('referer')->nullable();
127
                $table->string('language');
128
                $table->boolean('is_no_cache')->default(0);
129
                $table->boolean('wants_json')->default(0);
130
                $table->boolean('is_secure')->default(0);
131
                $table->boolean('is_json')->default(0);
132
                $table->boolean('is_ajax')->default(0);
133
                $table->boolean('is_pjax')->default(0);
134
                $table->timestamp('created_at')->nullable();
135
136
                // Indexes
137
                $table->foreign('route_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.routes'))
138
                    ->onDelete('cascade')->onUpdate('cascade');
139
                $table->foreign('agent_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.agents'))
140
                    ->onDelete('cascade')->onUpdate('cascade');
141
                $table->foreign('device_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.devices'))
142
                    ->onDelete('cascade')->onUpdate('cascade');
143
                $table->foreign('platform_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.platforms'))
144
                    ->onDelete('cascade')->onUpdate('cascade');
145
                $table->foreign('path_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.paths'))
146
                    ->onDelete('cascade')->onUpdate('cascade');
147
                $table->foreign('geoip_id')->references('id')->on(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.geoips'))
148
                    ->onDelete('cascade')->onUpdate('cascade');
149
            }
150
        );
151
    }
152
153
    /**
154
     * Reverse the migrations.
155
     *
156
     * @return void
157
     */
158
    public function down(): void
159
    {
160
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.requests'));
161
162
163
164
165
166
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.geoips'));
167
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.platforms'));
168
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.devices'));
169
170
171
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.agents'));
172
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.paths'));
173
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.routes'));
174
        Schema::dropIfExists(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.data'));
175
    }
176
177
    /**
178
     * Get jsonable column data type.
179
     *
180
     * @return string
181
     */
182
    protected function jsonable(): string
183
    {
184
        $driverName = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
185
        $dbVersion = DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
186
        $isOldVersion = version_compare($dbVersion, '5.7.8', 'lt');
187
188
        return $driverName === 'mysql' && $isOldVersion ? 'text' : 'json';
189
    }
190
}
191