Passed
Push — dependabot/npm_and_yarn/docs/p... ( 0a6d62 )
by
unknown
08:03
created

RefreshLQIP   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 27.78%

Importance

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