Completed
Push — develop ( 9ee49b...09fe33 )
by Martin
02:19
created

FileProcessor::getStoragePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Luminark\Rivet\Services;
4
5
use Luminark\Rivet\Interfaces\FileProcessorInterface;
6
use Illuminate\Contracts\Filesystem\Filesystem;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Contracts\Config\Repository as Config;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\File\File;
11
use Luminark\Rivet\Models\Rivet;
12
use Luminark\Rivet\Events\CopyingRivetFile;
13
use Luminark\Rivet\Events\CopiedRivetFile;
14
use Luminark\Rivet\Events\MovingRivetFile;
15
use Luminark\Rivet\Events\MovedRivetFile;
16
17
class FileProcessor implements FileProcessorInterface
18
{
19
    protected $filesystem;
20
    
21
    protected $dispatcher;
22
    
23
    protected $config;
24
    
25
    public function __construct(
26
        Filesystem $filesystem, 
27
        Dispatcher $dispatcher, 
28
        Config $config
29
    ) {
30
        $this->filesystem = $filesystem;
31
        $this->dispatcher = $dispatcher;
32
        $this->config = $config;
33
    }
34
    
35
    public function processFile(Rivet $rivet, $file)
36
    {
37
        if (is_string($file)) {
38
            $file = new File($file);
39
        }
40
        
41
        $this->dispatcher->fire(
42
            new CopyingRivetFile($rivet, $file)
43
        );
44
45
        $tempFile = $this->copyFile($file);
46
        
47
        $this->dispatcher->fire(
48
            new CopiedRivetFile($rivet, $tempFile)
49
        );
50
        
51
        $filePath = $this->getStoragePath(get_class($rivet)) 
52
            . DIRECTORY_SEPARATOR 
53
            . $tempFile->getBasename();
54
        while($this->filesystem->exists($filePath)) {
55
            $filePath = $this->generateUniqueFilename($filePath);
56
        }
57
        
58
        $this->dispatcher->fire(
59
            new MovingRivetFile($rivet, $tempFile)
60
        );
61
        
62
        $this->filesystem->put($filePath, file_get_contents($tempFile->getRealPath()));
63
        
64
        $this->dispatcher->fire(
65
            new MovedRivetFile($rivet, $filePath)
66
        );
67
        
68
        $fileInfo = $this->getFileInfo($filePath, $tempFile);
69
        unlink($tempFile->getRealPath());
70
        
71
        return $fileInfo;
72
    }
73
    
74
    protected function copyFile(File $file)
75
    {
76
        if ($file instanceof UploadedFile) {
77
            $filename = $file->getClientOriginalName();
78
        } else {
79
            $filename = $file->getFilename();
80
        }
81
        
82
        $tempStoragePath = $this->getStoragePath('temp');
83
        $tempPath = $tempStoragePath . DIRECTORY_SEPARATOR . $filename;
84
        
85
        if ( ! file_exists($tempStoragePath)) {
86
            mkdir($tempStoragePath, 0775, true);
87
        }
88
        
89
        copy($file->getRealPath(), $tempPath);
90
        
91
        return new File($tempPath);
92
    }
93
    
94
    protected function getStoragePath($class)
95
    {
96
        return $this->config->get(
97
            'luminark.rivet.storage.' . $class,
98
            $this->config->get('luminark.rivet.storage.default')
99
        );
100
    }
101
    
102
    protected function generateUniqueFilename($string)
103
    {
104
        $string = explode('.', $string);
105
        $lastIndex = count($string) - 1;
106
        $string[$lastIndex] = str_random(5) . '.' . $string[$lastIndex];
107
        
108
        return implode('.', $string);
109
    }
110
    
111
    protected function getFileInfo($filePath, File $file)
112
    {
113
        return (object) [
114
            'path' => $filePath,
115
            'name' => basename($filePath),
116
            'mime' => $file->getMimeType(),
117
            'size' => $this->filesystem->size($filePath)
118
        ];
119
    }
120
}