FileProcessor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
c 3
b 0
f 1
lcom 1
cbo 8
dl 0
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStoragePath() 0 7 1
A generateUniqueFilename() 0 8 1
A getFileInfo() 0 9 1
A __construct() 0 9 1
B processFile() 0 38 3
A copyFile() 0 19 3
1
<?php
2
3
namespace Luminark\Rivet\Services;
4
5
use Luminark\Rivet\Interfaces\FileProcessorInterface;
6
use Illuminate\Contracts\Filesystem\Factory as Storage;
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 $storage;
20
21
    protected $dispatcher;
22
23
    protected $config;
24
25
    public function __construct(
26
        Storage $storage,
27
        Dispatcher $dispatcher,
28
        Config $config
29
    ) {
30
        $this->storage = $storage;
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->storage->exists($filePath)) {
0 ignored issues
show
Bug introduced by
The method exists() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
55
            $filePath = $this->generateUniqueFilename($filePath);
56
        }
57
58
        $this->dispatcher->fire(
59
            new MovingRivetFile($rivet, $tempFile)
60
        );
61
62
        $this->storage->put($filePath, file_get_contents($tempFile->getRealPath()));
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
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->storage->size($filePath)
118
        ];
119
    }
120
}
121