EloquentFileRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 6 1
A createFromFile() 0 19 2
A destroy() 0 4 1
A findFileByZoneForEntity() 0 10 3
A findMultipleFilesByZoneForEntity() 0 11 3
1
<?php namespace Modules\Media\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Collection;
4
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
5
use Modules\Media\Entities\File;
6
use Modules\Media\Helpers\FileHelper;
7
use Modules\Media\Repositories\FileRepository;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class EloquentFileRepository extends EloquentBaseRepository implements FileRepository
11
{
12
    /**
13
     * Update a resource
14
     * @param  File  $file
15
     * @param $data
16
     * @return mixed
17
     */
18
    public function update($file, $data)
19
    {
20
        $file->update($data);
21
22
        return $file;
23
    }
24
25
    /**
26
     * Create a file row from the given file
27
     * @param  UploadedFile $file
28
     * @return mixed
29
     */
30
    public function createFromFile(UploadedFile $file)
31
    {
32
        $fileName = FileHelper::slug($file->getClientOriginalName());
33
34
        $exists = $this->model->whereFilename($fileName)->first();
35
36
        if ($exists) {
37
            throw new \InvalidArgumentException('File slug already exists');
38
        }
39
40
        return $this->model->create([
41
            'filename' => $fileName,
42
            'path' => config('asgard.media.config.files-path') . "{$fileName}",
43
            'extension' => $file->guessClientExtension(),
44
            'mimetype' => $file->getClientMimeType(),
45
            'filesize' => $file->getFileInfo()->getSize(),
46
            'folder_id' => 0,
47
        ]);
48
    }
49
50
    public function destroy($file)
51
    {
52
        $file->delete();
53
    }
54
55
    /**
56
     * Find a file for the entity by zone
57
     * @param $zone
58
     * @param object $entity
59
     * @return object
60
     */
61
    public function findFileByZoneForEntity($zone, $entity)
62
    {
63
        foreach ($entity->files as $file) {
64
            if ($file->pivot->zone == $zone) {
65
                return $file;
66
            }
67
        }
68
69
        return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type declared by the interface Modules\Media\Repositori...findFileByZoneForEntity of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
70
    }
71
72
    /**
73
     * Find multiple files for the given zone and entity
74
     * @param zone $zone
75
     * @param object $entity
76
     * @return object
77
     */
78
    public function findMultipleFilesByZoneForEntity($zone, $entity)
79
    {
80
        $files = [];
81
        foreach ($entity->files as $file) {
82
            if ($file->pivot->zone == $zone) {
83
                $files[] = $file;
84
            }
85
        }
86
87
        return new Collection($files);
88
    }
89
}
90