PurgeDeletedImages   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fire() 0 19 3
1
<?php
2
3
namespace OkayBueno\Images\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use OkayBueno\Images\Models\Image;
8
use OkayBueno\Images\Services\ImageServiceInterface;
9
10
/**
11
 * Class PurgeDeletedImages
12
 * @package OkayBueno\Images\Commands
13
 */
14
class PurgeDeletedImages extends Command
15
{
16
    protected $signature = 'images:purge-deleted-images {--days=30}';
17
    protected $description = 'Removes the images that were deleted more than the number of days specified in the command. ';
18
19
    protected $imageService;
20
21
    /**
22
     * @param ImageServiceInterface $imageServiceInterface
23
     */
24
    public function __construct(
25
        ImageServiceInterface $imageServiceInterface
26
    )
27
    {
28
        parent::__construct();
29
        $this->imageService = $imageServiceInterface;
30
    }
31
32
33
    /**
34
     *
35
     */
36
    public function fire()
37
    {
38
        $numberOfDays = (int)$this->option('days');
39
        $numberOfDays = $numberOfDays <= 0 ? 1 : $numberOfDays;
40
41
        $minDate = Carbon::now()->subDays( $numberOfDays );
42
43
        // get all the images that were removed at least $numberOfDays ago.
44
        $counter = -1;
45
        foreach( Image::where('deleted_at', '<=', $minDate )->withTrashed()->cursor() as $counter => $deletedImage  )
46
        {
47
            // Destroy image.
48
            $this->imageService->destroyImage( $deletedImage );
49
            $this->info("Image #$deletedImage->id was destroyed from disk and database.");
50
        }
51
52
        $counter++;
53
54
        $this->info("A total amount of $counter images were destroyed.");
55
    }
56
57
}