Test Failed
Push — master ( 711522...b3392a )
by Mostafa
59s queued 16s
created

Blueprint::addOriginalName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Mostafaznv\Larupload\Database\Schema;
4
5
use Illuminate\Database\Schema\Blueprint as BlueprintIlluminate;
6
use Illuminate\Support\Facades\DB;
7
use Mostafaznv\Larupload\Enums\LaruploadFileType;
8
use Mostafaznv\Larupload\Enums\LaruploadMode;
9
use PDO;
10
11
class Blueprint
12
{
13
    /**
14
     * Add upload columns to the table
15
     *
16
     * @param BlueprintIlluminate $table
17
     * @param string $name
18
     * @param LaruploadMode $mode
19
     */
20
    public static function columns(BlueprintIlluminate $table, string $name, LaruploadMode $mode = LaruploadMode::HEAVY): void
21
    {
22
        $table->string("{$name}_file_name", 255)->nullable();
23
24
        if ($mode === LaruploadMode::HEAVY) {
25
            $table->string("{$name}_file_id", 36)->nullable();
26
27
            if (config('larupload.store-original-file-name', false)) {
28
                $table->string("{$name}_file_original_name", 85)->nullable()->index();
29
            }
30
31
            $table->unsignedInteger("{$name}_file_size")->nullable()->index();
32
            $table->enum("{$name}_file_type", enum_to_names(LaruploadFileType::cases()))->nullable()->index();
33
            $table->string("{$name}_file_mime_type", 85)->nullable();
34
            $table->unsignedInteger("{$name}_file_width")->nullable();
35
            $table->unsignedInteger("{$name}_file_height")->nullable();
36
            $table->unsignedInteger("{$name}_file_duration")->nullable()->index();
37
            $table->string("{$name}_file_dominant_color", 7)->nullable();
38
            $table->string("{$name}_file_format", 85)->nullable();
39
            $table->string("{$name}_file_cover", 85)->nullable();
40
        }
41
        else {
42
            $table->{self::jsonColumnType()}("{$name}_file_meta")->nullable();
43
        }
44
    }
45
46
    /**
47
     * Drop upload columns
48
     *
49
     * @param BlueprintIlluminate $table
50
     * @param string $name
51
     * @param LaruploadMode $mode
52
     */
53
    public static function dropColumns(BlueprintIlluminate $table, string $name, LaruploadMode $mode = LaruploadMode::HEAVY): void
54
    {
55
        $columns = [
56
            "{$name}_file_name"
57
        ];
58
59
60
        if ($mode === LaruploadMode::HEAVY) {
61
            $tableName = $table->getTable();
62
            $heavyColumns = [
63
                "{$name}_file_id", "{$name}_file_size", "{$name}_file_type", "{$name}_file_mime_type",
64
                "{$name}_file_width", "{$name}_file_height", "{$name}_file_duration",
65
                "{$name}_file_dominant_color", "{$name}_file_format", "{$name}_file_cover"
66
            ];
67
68
            if (config('larupload.store-original-file-name', false)) {
69
                $heavyColumns[] = "{$name}_file_original_name";
70
71
                $table->dropIndex("{$tableName}_{$name}_file_original_name_index");
72
            }
73
74
75
            $columns = array_merge($columns, $heavyColumns);
76
77
            $table->dropIndex("{$tableName}_{$name}_file_size_index");
78
            $table->dropIndex("{$tableName}_{$name}_file_type_index");
79
            $table->dropIndex("{$tableName}_{$name}_file_duration_index");
80
        }
81
        else {
82
            $columns[] = "{$name}_file_meta";
83
        }
84
85
86
        $table->dropColumn($columns);
87
    }
88
89
    /**
90
     * Add upload file_original_name column to the table
91
     *
92
     * @param BlueprintIlluminate $table
93
     * @param string $name
94
     * @param LaruploadMode $mode
95
     */
96
    public static function addOriginalName(BlueprintIlluminate $table, string $name): void
97
    {
98
        $table->string("{$name}_file_original_name", 85)
99
            ->nullable()
100
            ->index()
101
            ->after("{$name}_file_id");
102
    }
103
104
105
    /**
106
     * Get json column data type
107
     *
108
     * @return string
109
     */
110
    protected static function jsonColumnType(): string
111
    {
112
        return DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql' && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge') ? 'json' : 'text';
113
    }
114
}
115