Completed
Push — dev ( e5a31e...27c800 )
by Darko
06:50
created

ReleaseFile::deleteReleaseFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\SphinxSearch;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Carbon;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * App\Models\ReleaseFile.
12
 *
13
 * @property int $releases_id FK to releases.id
14
 * @property string $name
15
 * @property int $size
16
 * @property bool $ishashed
17
 * @property \Carbon\Carbon|null $created_at
18
 * @property \Carbon\Carbon|null $updated_at
19
 * @property bool $passworded
20
 * @property-read \App\Models\Release $release
21
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereCreatedAt($value)
22
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereIshashed($value)
23
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereName($value)
24
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile wherePassworded($value)
25
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereReleasesId($value)
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereSize($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereUpdatedAt($value)
28
 * @mixin \Eloquent
29
 * @property string $crc32
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile newModelQuery()
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile newQuery()
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile query()
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseFile whereCrc32($value)
34
 */
35
class ReleaseFile extends Model
36
{
37
    /**
38
     * @var bool
39
     */
40
    protected $dateFormat = false;
41
42
    /**
43
     * @var array
44
     */
45
    protected $guarded = [];
46
47
    /**
48
     * @var string
49
     */
50
    protected $primaryKey = 'releases_id';
51
52
    /**
53
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
54
     */
55
    public function release(): \Illuminate\Database\Eloquent\Relations\BelongsTo
56
    {
57
        return $this->belongsTo(Release::class, 'releases_id');
58
    }
59
60
    /**
61
     * Get releasefiles row by id.
62
     *
63
     *
64
     * @param $id
65
     * @return \Illuminate\Database\Eloquent\Collection|static[]
66
     */
67
    public static function getReleaseFiles($id)
68
    {
69
        return self::query()->where('releases_id', $id)->orderBy('name')->get();
70
    }
71
72
    /**
73
     * @param $guid
74
     * @return \Illuminate\Database\Eloquent\Collection|static[]
75
     */
76
    public static function getByGuid($guid)
77
    {
78
        return self::query()
79
            ->join('releases', 'releases.id', '=', 'release_files.releases_id')
80
            ->where('releases.guid', $guid)
81
            ->orderBy('release_files.name')->get();
82
    }
83
84
    /**
85
     * Add new files for a release ID.
86
     *
87
     *
88
     * @param        $id
89
     * @param        $name
90
     * @param        $size
91
     * @param        $createdTime
92
     * @param        $hasPassword
93
     *
94
     * @param string $hash
95
     * @param string $crc
96
     * @return int
97
     * @throws \Exception
98
     */
99
    public static function addReleaseFiles($id, $name, $size, $createdTime, $hasPassword, $hash = '', $crc = ''): int
100
    {
101
        // Check if we already have this data in table
102
        $duplicateCheck = self::query()->where('releases_id', $id)->where('name', $name)->first();
103
104
        // Check if the release exists in releases table to prevent foreign key error
105
        $releaseCheck = Release::query()->where('id', $id)->first();
106
107
        if (is_int($createdTime)) {
108
            if ($createdTime === 0) {
109
                $adjustedCreatedTime = now()->format('Y-m-d H:i:s');
110
            } else {
111
                $adjustedCreatedTime = Carbon::createFromTimestamp($createdTime)->format('Y-m-d H:i:s');
112
            }
113
        } else {
114
            $adjustedCreatedTime = $createdTime;
115
        }
116
117
        if ($duplicateCheck === null && $releaseCheck !== null) {
118
            try {
119
                $insert = self::insertOrIgnore([
120
                        'releases_id' => $id,
121
                        'name' => $name,
122
                        'size' => $size,
123
                        'created_at' => $adjustedCreatedTime,
124
                        'updated_at' => now()->timestamp,
125
                        'passworded' => $hasPassword,
126
                        'crc32' => $crc,
127
                    ]);
128
            } catch (\PDOException $e) {
129
                Log::alert($e->getMessage());
130
            }
131
132
            if (\strlen($hash) === 32) {
133
                ParHash::insertOrIgnore(['releases_id' => $id, 'hash' => $hash]);
134
            }
135
            if (config('nntmux.elasticsearch_enabled') === true) {
136
                $new = Release::query()
137
                    ->where('releases.id', $id)
138
                    ->leftJoin('release_files as rf', 'releases.id', '=', 'rf.releases_id')
139
                    ->select(['releases.id', 'releases.name', 'releases.searchname', 'releases.fromname', DB::raw('IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename')])
0 ignored issues
show
Bug introduced by
The type App\Models\DB was not found. Did you mean DB? If so, make sure to prefix the type with \.
Loading history...
140
                    ->groupBy('releases.id')
141
                    ->first();
142
                if ($new !== null) {
143
                    $data = [
144
                        'body' => [
145
                            'doc' => [
146
                                'filename' => ! empty($new->filename) ? $new->filename : '',
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
147
                            ],
148
                        ],
149
150
                        'index' => 'releases',
151
                        'id' => $id,
152
                    ];
153
154
                    Elasticsearch::update($data);
0 ignored issues
show
Bug introduced by
The type App\Models\Elasticsearch 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...
155
                }
156
            } else {
157
                (new SphinxSearch())->updateRelease($id);
158
            }
159
        }
160
161
        return $insert ?? 0;
162
    }
163
}
164