EloquentPhotoRepository::all()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace JeroenG\LaravelPhotoGallery\Repositories;
2
3
use Illuminate\Http\Request;
4
use Illuminate\Contracts\Filesystem\Factory as Filesystem;
5
use JeroenG\LaravelPhotoGallery\Models\Photo;
6
7
class EloquentPhotoRepository implements PhotoRepository {
8
	
9
	public function all()
10
	{
11
		return Photo::all();
12
	}
13
14
	public function find($id)
15
	{
16
		return Photo::find($id);
17
	}
18
19
	public function findOrFail($id)
20
	{
21
		return Photo::findOrFail($id);
22
	}
23
24
	public function findByAlbumId($albumId)
25
	{
26
		return Photo::where('album_id', '=', $albumId)->paginate(10);
27
	}
28
29
	public function create(Request $request, Filesystem $storage)
30
	{
31
		$filename = str_random(10).time() .".". $request->file('photo_file')->getClientOriginalExtension();
32
        $storage->put('uploads/photos/'.$filename, \File::get($request->file('photo_file')));
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...
33
34
        $input = $request->input();
35
		$newPhoto = new Photo;
36
		$newPhoto->photo_name = $input['photo_name'];
37
		$newPhoto->photo_description = $input['photo_description'];
38
		$newPhoto->filename = $filename;
39
		$newPhoto->album_id = $input['album_id'];
40
		return $newPhoto->save();
41
	}
42
43
	public function update($id, $input)
44
	{
45
		$photo = Photo::find($id);
46
		$photo->photo_name = $input['photo_name'];
47
		$photo->photo_description = $input['photo_description'];
48
		$photo->album_id = $input['album_id'];
49
		$photo->touch();
50
		return $photo->save();
51
	}
52
53
	public function delete($id, Filesystem $storage)
54
	{
55
		$photo = Photo::find($id);
56
        $file = "uploads/photos/".$photo->filename;
57
        $this->checkDeletedDir($storage);
58
        $storage->move($file, "uploads/photos/deleted/".$photo->filename);
0 ignored issues
show
Bug introduced by
The method move() 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...
59
		return $photo->delete();
60
	}
61
62
	public function checkDeletedDir(Filesystem $storage)
63
	{
64
   		return $storage->exists("uploads/photos/deleted/") || $storage->makeDirectory("uploads/photos/deleted/");
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...
Bug introduced by
The method makeDirectory() 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...
65
	}
66
67
	public function forceDelete($id, Filesystem $storage)
68
	{
69
		$photo = Photo::find($id);
70
        if ($storage->exists("uploads/photos/deleted/".$photo->filename))
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...
71
        {
72
        	$storage->delete("uploads/photos/deleted/".$photo->filename);
0 ignored issues
show
Bug introduced by
The method delete() 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...
73
        }
74
        else
75
        {
76
        	$storage->delete("uploads/photos/".$photo->filename);
0 ignored issues
show
Bug introduced by
The method delete() 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...
77
        }
78
		return $photo->forceDelete();
79
	}
80
81
	public function restore($id, Filesystem $storage)
82
	{
83
		$photo = Photo::withTrashed()->find($id);
84
        $deletedFile = "uploads/photos/deleted/".$photo->filename;
85
        $storage->move($deletedFile, "uploads/photos/".$photo->filename);
0 ignored issues
show
Bug introduced by
The method move() 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...
86
		return $photo->restore();
87
	}
88
89
	public function restoreFromAlbum($albumId, Filesystem $storage)
90
	{
91
		$albumPhotos = Photo::withTrashed()->where('album_id', $albumId)->get();
92
93
		foreach ($albumPhotos as $photo) {
94
			$this->restore($photo->photo_id, $storage);
95
		}
96
	}
97
}