MediaLibraryEventHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribe() 0 7 1
A whenMediaHasBeenAdded() 0 12 1
1
<?php
2
3
namespace App\Services\Medialibrary;
4
5
use Spatie\Color\Rgb;
6
use ColorThief\ColorThief;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;
9
10
class MediaLibraryEventHandler
11
{
12
    public function subscribe(Dispatcher $events)
13
    {
14
        $events->listen(
15
            MediaHasBeenAdded::class,
16
            [$this, 'whenMediaHasBeenAdded']
17
        );
18
    }
19
20
    public function whenMediaHasBeenAdded(MediaHasBeenAdded $event)
21
    {
22
        $media = $event->media;
23
24
        $dominantColor = ColorThief::getColor($media->getPath());
25
26
        $hexColor = (new Rgb(...$dominantColor))->toHex();
0 ignored issues
show
Bug introduced by
The call to Rgb::__construct() misses some required arguments starting with $green.
Loading history...
Documentation introduced by
$dominantColor is of type array|boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
28
        $media->setCustomProperty('dominantColor', $hexColor);
29
30
        $media->save();
31
    }
32
}
33