Passed
Push — master ( bb706c...93a2f5 )
by Quentin
06:42
created

RefreshLQIP   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 29.4%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 66
ccs 5
cts 17
cp 0.294
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 20 6
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use A17\Twill\Models\Media;
6
use Illuminate\Config\Repository as Config;
7
use Illuminate\Console\Command;
8
use Illuminate\Database\DatabaseManager;
9
use Illuminate\Support\Arr;
10
use ImageService;
0 ignored issues
show
Bug introduced by
The type ImageService 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...
11
12
class RefreshLQIP extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'twill:lqip {--all=0}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Refresh Low Quality Image Placeholders.';
27
28
    /**
29
     * @var DatabaseManager
30
     */
31
    protected $db;
32
33
    /**
34
     * @var Config
35
     */
36
    protected $config;
37
38
    protected $cropParamsKeys = [
39
        'crop_x',
40
        'crop_y',
41
        'crop_w',
42
        'crop_h',
43
    ];
44
45
    /**
46
     * @param DatabaseManager $db
47
     * @param Config $config
48
     */
49 55
    public function __construct(DatabaseManager $db, Config $config)
50
    {
51 55
        parent::__construct();
52
53 55
        $this->db = $db;
54 55
        $this->config = $config;
55 55
    }
56
57
    // TODO: document this and actually think about moving to queuable job after content type updates
58
    public function handle()
59
    {
60
        $this->db->table(config('twill.mediables_table', 'twill_mediables'))->orderBy('id')->chunk(100, function ($attached_medias) {
61
            foreach ($attached_medias as $attached_media) {
62
                $uuid = Media::withTrashed()->find($attached_media->media_id, ['uuid'])->uuid;
63
64
                $lqip_width = $this->config->get('lqip.' . $attached_media->mediable_type . '.' . $attached_media->role . '.' . $attached_media->crop, 30);
65
66
                if ($lqip_width && (!$attached_media->lqip_data || $this->option('all'))) {
67
68
                    $crop_params = Arr::only((array) $attached_media, $this->cropParamsKeys);
69
70
                    $url = ImageService::getLQIPUrl($uuid, $crop_params + ['w' => $lqip_width]);
71
72
                    try {
73
                        $data = file_get_contents($url);
74
                        $dataUri = 'data:image/gif;base64,' . base64_encode($data);
75
                        $this->db->table(config('twill.mediables_table', 'twill_mediables'))->where('id', $attached_media->id)->update(['lqip_data' => $dataUri]);
76
                    } catch (\Exception $e) {
77
                        $this->info("LQIP was not generated for " . $uuid);
78
                    }
79
                }
80
            }
81
        });
82
    }
83
}
84