Completed
Push — dev ( bc169d...1438cf )
by Darko
07:26
created

Predb::getAll()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 58
ccs 0
cts 20
cp 0
rs 8.0995
cc 8
nc 6
nop 1
crap 72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\ColorCLI;
6
use Blacklight\ConsoleTools;
7
use Blacklight\SphinxSearch;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Facades\Cache;
11
use Laravel\Scout\Searchable;
12
13
/**
14
 * App\Models\Predb.
15
 *
16
 * @property mixed $release
17
 * @property mixed $hash
18
 * @property int $id Primary key
19
 * @property string $title
20
 * @property string|null $nfo
21
 * @property string|null $size
22
 * @property string|null $category
23
 * @property string|null $predate
24
 * @property string $source
25
 * @property int $requestid
26
 * @property int $groups_id FK to groups
27
 * @property bool $nuked Is this pre nuked? 0 no 2 yes 1 un nuked 3 mod nuked
28
 * @property string|null $nukereason If this pre is nuked, what is the reason?
29
 * @property string|null $files How many files does this pre have ?
30
 * @property string $filename
31
 * @property bool $searched
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereCategory($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFilename($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFiles($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereGroupsId($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereId($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNfo($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNuked($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNukereason($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb wherePredate($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereRequestid($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSearched($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSize($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSource($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereTitle($value)
46
 * @mixin \Eloquent
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newModelQuery()
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newQuery()
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb query()
50
 */
51
class Predb extends Model
52
{
53
    use Searchable;
0 ignored issues
show
Bug introduced by
The trait Laravel\Scout\Searchable requires the property $queryCallback which is not provided by App\Models\Predb.
Loading history...
54
55
    // Nuke status.
56
    public const PRE_NONUKE = 0; // Pre is not nuked.
57
    public const PRE_UNNUKED = 1; // Pre was un nuked.
58
    public const PRE_NUKED = 2; // Pre is nuked.
59
    public const PRE_MODNUKE = 3; // Nuke reason was modified.
60
    public const PRE_RENUKED = 4; // Pre was re nuked.
61
    public const PRE_OLDNUKE = 5; // Pre is nuked for being old.
62
63
    /**
64
     * @var string
65
     */
66
    protected $table = 'predb';
67
68
    /**
69
     * @var bool
70
     */
71
    public $timestamps = false;
72
73
    /**
74
     * @var bool
75
     */
76
    protected $dateFormat = false;
77
78
    /**
79
     * @var array
80
     */
81
    protected $guarded = [];
82
83
    public function hash()
84
    {
85
        return $this->hasMany(PredbHash::class, 'predb_id');
86
    }
87
88
    public function release()
89
    {
90
        return $this->hasMany(Release::class, 'predb_id');
91
    }
92
93
    /**
94
     * Attempts to match PreDB titles to releases.
95
     *
96
     * @param string|int|bool $dateLimit
97
     * @throws \RuntimeException
98
     */
99
    public static function checkPre($dateLimit = false): void
100
    {
101
        $consoleTools = new ConsoleTools();
102
        $updated = 0;
103
104
        if (config('nntmux.echocli')) {
105
            (new ColorCLI())->header('Querying DB for release search names not matched with PreDB titles.');
106
        }
107
108
        $query = self::query()
109
            ->where('releases.predb_id', '<', 1)
110
            ->join('releases', 'predb.title', '=', 'releases.searchname')
111
            ->select(['predb.id as predb_id', 'releases.id as releases_id']);
112
        if ($dateLimit !== false && (int) $dateLimit > 0) {
113
            $query->where('adddate', '>', now()->subDays((int) $dateLimit));
114
        }
115
116
        $res = $query->get();
117
118
        if ($res !== null) {
119
            $total = \count($res);
120
            (new ColorCLI())->primary(number_format($total).' releases to match.');
121
122
            foreach ($res as $row) {
123
                Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]);
124
125
                if (config('nntmux.echocli')) {
126
                    $consoleTools->overWritePrimary(
127
                        'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total)
128
                        );
129
                }
130
            }
131
            if (config('nntmux.echocli')) {
132
                echo PHP_EOL;
133
            }
134
135
            if (config('nntmux.echocli')) {
136
                (new ColorCLI())->header(
137
                    'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.'
138
                );
139
            }
140
        }
141
    }
142
143
    /**
144
     * Try to match a single release to a PreDB title when the release is created.
145
     *
146
     * @param string $cleanerName
147
     *
148
     * @return array|false Array with title/id from PreDB if found, false if not found.
149
     */
150
    public static function matchPre($cleanerName)
151
    {
152
        if (empty($cleanerName)) {
153
            return false;
154
        }
155
156
        $titleCheck = self::query()->where('title', $cleanerName)->first(['id']);
157
158
        if ($titleCheck !== null) {
159
            return [
160
                'title' => $cleanerName,
161
                'predb_id' => $titleCheck['id'],
162
            ];
163
        }
164
165
        // Check if clean name matches a PreDB filename.
166
        $fileCheck = self::query()->where('filename', $cleanerName)->first(['id', 'title']);
167
168
        if ($fileCheck !== null) {
169
            return [
170
                'title' => $fileCheck['title'],
171
                'predb_id' => $fileCheck['id'],
172
            ];
173
        }
174
175
        return false;
176
    }
177
178
    /**
179
     * @param string $search
180
     *
181
     * @return mixed
182
     * @throws \Exception
183
     */
184
    public static function getAll($search = '')
185
    {
186
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
187
        $predb = Cache::get(md5($search));
188
        if ($predb !== null) {
189
            return $predb;
190
        }
191
        $sql = self::query()->leftJoin('releases', 'releases.predb_id', '=', 'predb.id')->orderByDesc('predb.predate');
192
        if (! empty($search)) {
193
            if (config('nntmux.elasticsearch_enabled') === true) {
194
                $search = [
195
                    'scroll' => '30s',
196
                    'index' => 'predb',
197
                    'body' => [
198
                        'query' => [
199
                            'query_string' => [
200
                                'query' => $search,
201
                                'fields' => ['title'],
202
                                'analyze_wildcard' => true,
203
                                'default_operator' => 'and',
204
                            ],
205
                        ],
206
                        'size' => 1000,
207
                    ],
208
                ];
209
210
                $results = \Elasticsearch::search($search);
211
212
                $ids = [];
213
                while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
214
                    foreach ($results['hits']['hits'] as $result) {
215
                        $ids[] = $result['_source']['id'];
216
                    }
217
                    if (empty($ids)) {
218
                        return collect();
219
                    }
220
                    // When done, get the new scroll_id
221
                    // You must always refresh your _scroll_id!  It can change sometimes
222
                    $scroll_id = $results['_scroll_id'];
223
224
                    // Execute a Scroll request and repeat
225
                    $results = \Elasticsearch::scroll([
226
                            'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
227
                            'scroll'    => '30s',        // and the same timeout window
228
                        ]
229
                    );
230
                }
231
            } else {
232
                $sphinx = new SphinxSearch();
233
                $ids = Arr::pluck($sphinx->searchIndexes('predb_rt', $search, ['title']), 'id');
234
            }
235
            $sql->whereIn('predb.id', $ids);
236
        }
237
238
        $predb = $sql->paginate(config('nntmux.items_per_page'));
239
        Cache::put(md5($search), $predb, $expiresAt);
240
241
        return $predb;
242
    }
243
244
    /**
245
     * Get all PRE's for a release.
246
     *
247
     *
248
     * @param $preID
249
     * @return \Illuminate\Database\Eloquent\Collection|static[]
250
     */
251
    public static function getForRelease($preID)
252
    {
253
        return self::query()->where('id', $preID)->get();
254
    }
255
256
    /**
257
     * Return a single PRE for a release.
258
     *
259
     *
260
     * @param $preID
261
     * @return \Illuminate\Database\Eloquent\Model|null|static
262
     */
263
    public static function getOne($preID)
264
    {
265
        return self::query()->where('id', $preID)->first();
266
    }
267
268
    /**
269
     * @return string
270
     */
271
    public function searchableAs()
272
    {
273
        return 'ft_predb_filename';
274
    }
275
276
    /**
277
     * @return array
278
     */
279
    public function toSearchableArray()
280
    {
281
        return [
282
            'filename' => $this->filename,
283
        ];
284
    }
285
}
286