Passed
Push — master ( 1b988a...8cef26 )
by Darko
11:12
created

deleteFinishedAndOrphans()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 90
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
c 1
b 0
f 0
dl 0
loc 90
rs 6.7042
cc 11
nc 2
nop 1

How to fix   Long Method    Complexity   

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\Services;
4
5
use App\Models\Collection;
6
use App\Models\Settings;
7
use Blacklight\ColorCLI;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Str;
10
11
class CollectionCleanupService
12
{
13
    public function __construct(
14
        private readonly ColorCLI $colorCLI
15
    ) {}
16
17
    /**
18
     * Deletes finished/old collections, cleans orphans, and removes collections missed after NZB creation.
19
     * Mirrors the previous ProcessReleases::deleteCollections logic.
20
     *
21
     * @return int total deleted rows across operations (approximate)
22
     */
23
    public function deleteFinishedAndOrphans(bool $echoCLI): int
24
    {
25
        $startTime = now()->toImmutable();
26
        $deletedCount = 0;
27
28
        if ($echoCLI) {
29
            echo $this->colorCLI->header('Process Releases -> Delete finished collections.'.PHP_EOL).
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->colorCLI->header(.... App\Services\PHP_EOL) targeting Blacklight\ColorCLI::header() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure $this->colorCLI->header(.... App\Services\PHP_EOL) of type void can be used in concatenation? ( Ignorable by Annotation )

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

29
            echo /** @scrutinizer ignore-type */ $this->colorCLI->header('Process Releases -> Delete finished collections.'.PHP_EOL).
Loading history...
30
                $this->colorCLI->primary(sprintf(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->colorCLI->primary...etentionhours')), true) targeting Blacklight\ColorCLI::primary() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
31
                    'Deleting collections/binaries/parts older than %d hours.',
32
                    Settings::settingValue('partretentionhours')
33
                ), true);
34
        }
35
36
        DB::transaction(function () use (&$deletedCount, $startTime, $echoCLI) {
37
            $deleted = 0;
38
            $deleteQuery = Collection::query()
39
                ->where('dateadded', '<', now()->subHours(Settings::settingValue('partretentionhours')))
40
                ->delete();
41
            if ($deleteQuery > 0) {
42
                $deleted = $deleteQuery;
43
                $deletedCount += $deleted;
44
            }
45
            $firstQuery = $fourthQuery = now();
46
47
            $totalTime = $firstQuery->diffInSeconds($startTime, true);
48
49
            if ($echoCLI) {
50
                $this->colorCLI->primary(
51
                    'Finished deleting '.$deleted.' old collections/binaries/parts in '.
52
                    $totalTime.Str::plural(' second', $totalTime),
53
                    true
54
                );
55
            }
56
57
            if (random_int(0, 200) <= 1) {
58
                if ($echoCLI) {
59
                    echo $this->colorCLI->header('Process Releases -> Remove CBP orphans.'.PHP_EOL).
0 ignored issues
show
Bug introduced by
Are you sure $this->colorCLI->header(.... App\Services\PHP_EOL) of type void can be used in concatenation? ( Ignorable by Annotation )

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

59
                    echo /** @scrutinizer ignore-type */ $this->colorCLI->header('Process Releases -> Remove CBP orphans.'.PHP_EOL).
Loading history...
Bug introduced by
Are you sure the usage of $this->colorCLI->header(.... App\Services\PHP_EOL) targeting Blacklight\ColorCLI::header() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
                        $this->colorCLI->primary('Deleting orphaned collections.');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->colorCLI->primary...orphaned collections.') targeting Blacklight\ColorCLI::primary() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
                }
62
63
                $deleted = 0;
64
                $deleteQuery = Collection::query()
65
                    ->whereNull('binaries.id')
66
                    ->orWhereNull('parts.binaries_id')
67
                    ->leftJoin('binaries', 'collections.id', '=', 'binaries.collections_id')
68
                    ->leftJoin('parts', 'binaries.id', '=', 'parts.binaries_id')
69
                    ->delete();
70
71
                if ($deleteQuery > 0) {
72
                    $deleted = $deleteQuery;
73
                    $deletedCount += $deleted;
74
                }
75
76
                $totalTime = now()->diffInSeconds($firstQuery);
77
78
                if ($echoCLI) {
79
                    $this->colorCLI->primary('Finished deleting '.$deleted.' orphaned collections in '.$totalTime.Str::plural(' second', $totalTime), true);
80
                }
81
            }
82
83
            if ($echoCLI) {
84
                $this->colorCLI->primary('Deleting collections that were missed after NZB creation.', true);
85
            }
86
87
            $deleted = 0;
88
            $collections = Collection::query()
89
                ->where('releases.nzbstatus', '=', 1)
90
                ->leftJoin('releases', 'releases.id', '=', 'collections.releases_id')
91
                ->select('collections.id')
92
                ->get();
93
94
            foreach ($collections as $collection) {
95
                $deleted++;
96
                Collection::query()->where('id', $collection->id)->delete();
97
            }
98
            $deletedCount += $deleted;
99
100
            $colDelTime = now()->diffInSeconds($fourthQuery, true);
101
            $totalTime = $fourthQuery->diffInSeconds($startTime, true);
102
103
            if ($echoCLI) {
104
                $this->colorCLI->primary(
105
                    'Finished deleting '.$deleted.' collections missed after NZB creation in '.$colDelTime.Str::plural(' second', $colDelTime).
106
                    PHP_EOL.'Removed '.number_format($deletedCount).' parts/binaries/collection rows in '.$totalTime.Str::plural(' second', $totalTime),
107
                    true
108
                );
109
            }
110
        }, 10);
111
112
        return $deletedCount;
113
    }
114
}
115