Completed
Push — master ( f2fe55...d64f0f )
by Freek
17:48
created

Filesystem   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 15
Bugs 2 Features 5
Metric Value
wmc 11
c 15
b 2
f 5
lcom 1
cbo 8
dl 0
loc 151
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 8 1
A copyToMediaLibrary() 0 16 3
A getRemoteHeadersForFile() 0 8 1
A copyFromMediaLibrary() 0 10 1
A removeFiles() 0 4 1
A renameFile() 0 9 1
A getMediaDirectory() 0 12 2
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Contracts\Config\Repository as ConfigRepository;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Contracts\Filesystem\Factory;
8
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;
9
use Spatie\MediaLibrary\Helpers\File;
10
use Spatie\MediaLibrary\PathGenerator\PathGeneratorFactory;
11
12
class Filesystem
13
{
14
    /**
15
     * @var \Illuminate\Contracts\Filesystem\Factory
16
     */
17
    protected $filesystem;
18
19
    /**
20
     * @var \Illuminate\Contracts\Config\Repository
21
     */
22
    protected $config;
23
24
    /**
25
     * @var \Illuminate\Contracts\Events\Dispatcher
26
     */
27
    protected $events;
28
29
    /**
30
     * @param Factory                                 $filesystems
31
     * @param \Illuminate\Contracts\Config\Repository $config
32
     */
33
    public function __construct(Factory $filesystems, ConfigRepository $config, Dispatcher $events)
34
    {
35
        $this->filesystem = $filesystems;
36
        $this->config = $config;
37
        $this->events = $events;
38
    }
39
40
    /**
41
     * Add a file to the mediaLibrary for the given media.
42
     *
43
     * @param string                     $file
44
     * @param \Spatie\MediaLibrary\Media $media
45
     * @param string                     $targetFileName
46
     */
47
    public function add($file, Media $media, $targetFileName = '')
48
    {
49
        $this->copyToMediaLibrary($file, $media, false, $targetFileName);
50
51
        $this->events->fire(new MediaHasBeenAdded($media));
52
53
        app(FileManipulator::class)->createDerivedFiles($media);
54
    }
55
56
    /**
57
     * Copy a file to the mediaLibrary for the given $media.
58
     *
59
     * @param string                     $file
60
     * @param \Spatie\MediaLibrary\Media $media
61
     * @param bool                       $conversions
62
     * @param string                     $targetFileName
63
     */
64
    public function copyToMediaLibrary($file, Media $media, $conversions = false, $targetFileName = '')
65
    {
66
        $destination = $this->getMediaDirectory($media, $conversions).
67
            ($targetFileName == '' ? pathinfo($file, PATHINFO_BASENAME) : $targetFileName);
68
69
        if ($media->getDiskDriverName() === 'local') {
70
            $this->filesystem
71
                ->disk($media->disk)
0 ignored issues
show
Documentation introduced by
The property disk does not exist on object<Spatie\MediaLibrary\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...
72
                ->put($destination, fopen($file, 'r+'));
73
        } else {
74
            $this->filesystem
75
                ->disk($media->disk)
0 ignored issues
show
Documentation introduced by
The property disk does not exist on object<Spatie\MediaLibrary\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...
76
                ->getDriver()
77
                ->put($destination, fopen($file, 'r+'), $this->getRemoteHeadersForFile($file));
78
        }
79
    }
80
81
    /**
82
     * Get the headers to be used when copying the
83
     * given file to a remote filesytem.
84
     *
85
     * @param string $file
86
     *
87
     * @return array
88
     */
89
    public function getRemoteHeadersForFile($file)
90
    {
91
        $mimeTypeHeader = ['ContentType' => File::getMimeType($file)];
92
93
        $extraHeaders = $this->config->get('laravel-medialibrary.remote.extra_headers');
94
95
        return array_merge($mimeTypeHeader, $extraHeaders);
96
    }
97
98
    /**
99
     * Copy a file from the mediaLibrary to the given targetFile.
100
     *
101
     * @param \Spatie\MediaLibrary\Media $media
102
     * @param string                     $targetFile
103
     */
104
    public function copyFromMediaLibrary(Media $media, $targetFile)
105
    {
106
        $sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name;
107
108
        touch($targetFile);
109
110
        $stream = $this->filesystem->disk($media->disk)->readStream($sourceFile);
111
        file_put_contents($targetFile, stream_get_contents($stream), FILE_APPEND);
112
        fclose($stream);
113
    }
114
115
    /**
116
     * Remove all files for the given media.
117
     *
118
     * @param \Spatie\MediaLibrary\Media $media
119
     */
120
    public function removeFiles(Media $media)
121
    {
122
        $this->filesystem->disk($media->disk)->deleteDirectory($this->getMediaDirectory($media));
123
    }
124
125
    /**
126
     * Rename a file for the given media.
127
     *
128
     * @param Media  $media
129
     * @param string $oldName
130
     *
131
     * @return bool
132
     */
133
    public function renameFile(Media $media, $oldName)
134
    {
135
        $oldFile = $this->getMediaDirectory($media).'/'.$oldName;
136
        $newFile = $this->getMediaDirectory($media).'/'.$media->file_name;
137
138
        $this->filesystem->disk($media->disk)->move($oldFile, $newFile);
139
140
        return true;
141
    }
142
143
    /**
144
     * Return the directory where all files of the given media are stored.
145
     *
146
     * @param \Spatie\MediaLibrary\Media $media
147
     *
148
     * @return string
149
     */
150
    public function getMediaDirectory(Media $media, $conversion = false)
151
    {
152
        $pathGenerator = PathGeneratorFactory::create();
153
154
        $directory = $conversion ?
155
            $pathGenerator->getPathForConversions($media) :
156
            $pathGenerator->getPath($media);
157
158
        $this->filesystem->disk($media->disk)->makeDirectory($directory);
159
160
        return $directory;
161
    }
162
}
163