Passed
Push — dev5 ( 5d8b6d...b120ee )
by Ron
08:47
created

garbageCollection::cleanupFileLinks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 19
ccs 0
cts 9
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\CustomerSystems;
6
use App\UserSettings;
7
use Exception;
8
use Illuminate\Console\Command;
9
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\File;
12
use Illuminate\Support\Facades\Storage;
13
use Illuminate\Support\Facades\Artisan;
14
use Illuminate\Support\Facades\Queue;
15
use Symfony\Component\Console\Output\BufferedOutput;
16
use Carbon\Carbon;
17
use App\FileLinks;
18
use App\User;
19
20
class garbageCollection extends Command
21
{
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'garbagecollection:run';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Nightly maintenance script to wipe temporary and deleted data';
35
36
    /**
37
     * Create a new command instance.
38
     *
39
     * @return void
40
     */
41 2
    public function __construct()
42
    {
43 2
        parent::__construct();
44 2
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return mixed
50
     */
51
    public function handle()
52
    {
53
        Log::notice('Running Garbage Collection Job');
54
        $this->call('down');
55
        $this->retryQueue();
56
        $this->checkChunkFolder();
57
        $this->checkArchiveFolder();
58
        $this->cleanupCustomerSystems();
59
        $this->cleanupFileLinks();
60
        $this->call('up');
61
        Log::notice('Garbage Collection Job completed');
62
    }
63
64
    //  Check for any failed jobs in the work queue
65
    protected function retryQueue()
66
    {
67
        try
68
        {
69
            Log::info('Restarting Worker Queues');
70
            $this->call('queue:restart');
71
            Log::info('Retrying failed jobs');
72
            $this->call('queue:retry', ['id' => 'all']);
73
        }
74
        catch(Exception $e)
75
        {
76
            report($e);
77
        }
78
    }
79
80
    //  Determine if there are any files held over in the file chunks folder
81
    protected function checkChunkFolder()
82
    {
83
        $files = Storage::files('chunks');
84
85
        if(count($files) > 0)
86
        {
87
            try
88
            {
89
                Log::info('Found partial files in the "chunks" folder');
90
                Log::info('File List - ', array($files));
91
                Storage::deleteDirectory('chunks');
92
            }
93
            catch(Exception $e)
94
            {
95
                report($e);
96
            }
97
        }
98
    }
99
100
    //  Determine if there are any files held over in the archive_downlaods folder
101
    protected function checkArchiveFolder()
102
    {
103
        $files = Storage::files('archive_downloads');
104
105
        if(count($files) > 0)
106
        {
107
            try
108
            {
109
                Log::info('Found files in the "archive_downloads" folder');
110
                Log::debug('File List - ', array($files));
111
                Storage::deleteDirectory('archive_downloads');
112
            }
113
            catch(Exception $e)
114
            {
115
                report($e);
116
            }
117
        }
118
    }
119
120
    //  Perminately remove any customer systems that were deleted
121
    protected function cleanupCustomerSystems()
122
    {
123
        $sysList = CustomerSystems::onlyTrashed()->get();
124
125
        if($sysList->count() > 0)
126
        {
127
            Log::info('Cleaning up Customer Systems');
128
            $count = $sysList->count();
129
            foreach($sysList as $sys)
130
            {
131
                $sys->forceDelete();
132
            }
133
            Log::info($count.' systems permanently deleted');
134
        }
135
    }
136
137
    //  Cleanup any file links that are older than 30 days
138
    protected function cleanupFileLinks()
139
    {
140
        //  Get the list of users that want their links cleaned up
141
        $users = UserSettings::where('auto_del_link', true)->with('User')->get();
142
143
        //  Cycle through the list and get any any links that are more than 30 days old
144
        foreach($users as $user)
145
        {
146
            $links = FileLinks::where('user_id', $user->user_id)->where('expire', '<', Carbon::now()->subDays(30)->format('Y-m-d'))->get();
147
            $count = $links->count();
148
149
            if($count > 0)
150
            {
151
                foreach($links as $link)
152
                {
153
                    $link->delete();
154
                }
155
156
                Log::info($count.' file links for '.$user->User->full_name.' deleted for being expired more than 30 days');
157
            }
158
        }
159
    }
160
}
161