Completed
Pull Request — master (#41)
by
unknown
02:46
created

FileService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 1
cbo 6
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A store() 0 15 1
A createThumbnails() 0 4 1
A getDestinationPath() 0 8 2
A getConfiguredFilesystem() 0 4 1
1
<?php namespace Modules\Media\Services;
2
3
use Illuminate\Contracts\Filesystem\Factory;
4
use Illuminate\Foundation\Bus\DispatchesJobs;
5
use Modules\Media\Entities\File;
6
use Modules\Media\Jobs\CreateThumbnails;
7
use Modules\Media\Repositories\FileRepository;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class FileService
11
{
12
    use DispatchesJobs;
13
14
    /**
15
     * @var FileRepository
16
     */
17
    private $file;
18
    /**
19
     * @var Factory
20
     */
21
    private $filesystem;
22
23
    public function __construct(FileRepository $file, Factory $filesystem)
24
    {
25
        $this->file = $file;
26
        $this->filesystem = $filesystem;
27
    }
28
29
    /**
30
     * @param  UploadedFile $file
31
     * @return mixed
32
     */
33
    public function store(UploadedFile $file)
34
    {
35
        $savedFile = $this->file->createFromFile($file);
36
37
        $path = $this->getDestinationPath($savedFile->getOriginal('path'));
38
        $stream = fopen($file->getRealPath(), 'r+');
39
        $this->filesystem->disk($this->getConfiguredFilesystem())->writeStream($path, $stream, [
0 ignored issues
show
Bug introduced by
The method writeStream() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            'visibility' => 'public',
41
            'mimetype' => $savedFile->mimetype,
42
        ]);
43
44
        $this->createThumbnails($savedFile);
45
46
        return $savedFile;
47
    }
48
49
    /**
50
     * Create the necessary thumbnails for the given file
51
     * @param $savedFile
52
     */
53
    private function createThumbnails(File $savedFile)
54
    {
55
        $this->dispatch(new CreateThumbnails($savedFile->path));
56
    }
57
58
    /**
59
     * @param string $path
60
     * @return string
61
     */
62
    private function getDestinationPath($path)
63
    {
64
        if ($this->getConfiguredFilesystem() === 'local') {
65
            return basename(public_path()) . $path;
66
        }
67
68
        return $path;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    private function getConfiguredFilesystem()
75
    {
76
        return config('asgard.media.config.filesystem');
77
    }
78
}
79