Completed
Pull Request — master (#34)
by Sebastian
04:04
created

MediaLibraryEventHandler::whenMediaHasBeenAdded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services\Medialibrary;
4
5
use ColorThief\ColorThief;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;
8
9
class MediaLibraryEventHandler
10
{
11
    public function subscribe(Dispatcher $events)
12
    {
13
        $events->listen(
14
            MediaHasBeenAdded::class,
15
            [$this, 'whenMediaHasBeenAdded']
16
        );
17
    }
18
19
    public function whenMediaHasBeenAdded(MediaHasBeenAdded $event)
20
    {
21
        $media = $event->media;
22
23
        $dominantColor = ColorThief::getColor($media->getPath());
24
25
        $media->setCustomProperty('dominantColor', rgb_to_hex(...$dominantColor));
0 ignored issues
show
Bug introduced by
The call to rgb_to_hex() 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...
26
27
        $media->save();
28
    }
29
}
30