Completed
Push — master ( 07e250...146355 )
by Freek
22:02
created

src/MediaObserver.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Spatie\MediaLibrary\Models\Media;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Spatie\MediaLibrary\Filesystem\Filesystem;
8
9
class MediaObserver
10
{
11
    public function creating(Media $media)
12
    {
13
        $media->setHighestOrderNumber();
14
    }
15
16
    public function updating(Media $media)
17
    {
18
        if ($media->file_name !== $media->getOriginal('file_name')) {
19
            app(Filesystem::class)->syncFileNames($media);
20
        }
21
    }
22
23
    public function updated(Media $media)
24
    {
25
        if (is_null($media->getOriginal('model_id'))) {
26
            return;
27
        }
28
29
        if ($media->manipulations !== json_decode($media->getOriginal('manipulations'), true)) {
0 ignored issues
show
The property manipulations does not exist on object<Spatie\MediaLibrary\Models\Media>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
            $eventDispatcher = Media::getEventDispatcher();
31
            Media::unsetEventDispatcher();
32
33
            app(FileManipulator::class)->createDerivedFiles($media);
34
35
            Media::setEventDispatcher($eventDispatcher);
36
        }
37
    }
38
39
    public function deleted(Media $media)
40
    {
41
        if (in_array(SoftDeletes::class, class_uses_recursive($media))) {
42
            if (! $media->isForceDeleting()) {
43
                return;
44
            }
45
        }
46
47
        app(Filesystem::class)->removeAllFiles($media);
48
    }
49
}
50