FileService::createThumbnails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Modules\Media\Services;
2
3
use Illuminate\Contracts\Config\Repository;
4
use Illuminate\Contracts\Queue\Queue;
5
use Illuminate\Queue\Jobs\Job;
6
use Modules\Media\Repositories\FileRepository;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
class FileService
10
{
11
    /**
12
     * @var FileRepository
13
     */
14
    private $file;
15
    /**
16
     * @var Repository
17
     */
18
    private $config;
19
    /**
20
     * @var Queue
21
     */
22
    private $queue;
23
24
    public function __construct(
25
        FileRepository $file,
26
        Repository $config,
27
        Queue $queue)
28
    {
29
        $this->file = $file;
30
        $this->config = $config;
31
        $this->queue = $queue;
32
    }
33
34
    /**
35
     * @param  UploadedFile $file
36
     * @return mixed
37
     */
38
    public function store(UploadedFile $file)
39
    {
40
        // Save the file info to db
41
        try {
42
            $savedFile = $this->file->createFromFile($file);
43
        } catch (\InvalidArgumentException $e) {
44
            return $e->getMessage();
45
        }
46
47
        // Move the uploaded file to files path
48
        $file->move(public_path() . $this->config->get('asgard.media.config.files-path'), $savedFile->filename);
49
        @chmod(public_path() . $this->config->get('asgard.media.config.files-path') . $savedFile->filename, 0666);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
51
        $this->createThumbnails($savedFile);
52
53
        return $savedFile;
54
    }
55
56
    /**
57
     * Create the necessary thumbnails for the given file
58
     * @param $savedFile
59
     */
60
    private function createThumbnails($savedFile)
61
    {
62
        $this->queue->push(function (Job $job) use ($savedFile) {
0 ignored issues
show
Documentation introduced by
function (\Illuminate\Qu...; $job->delete(); } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
            app('imagy')->createAll($savedFile->path);
64
            $job->delete();
65
        });
66
    }
67
}
68