EloquentAlbumAdapter::findHidden()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace JeroenG\LaravelPhotoGallery\Adapters\Eloquent;
4
5
use Illuminate\Support\Collection;
6
use JeroenG\LaravelPhotoGallery\Models\Album;
7
use JeroenG\LaravelPhotoGallery\Entities as Entity;
8
use JeroenG\LaravelPhotoGallery\Contracts\AlbumAdapter;
9
use JeroenG\LaravelPhotoGallery\Contracts\PhotoAdapter;
10
11
class EloquentAlbumAdapter implements AlbumAdapter
12
{
13
    private $photoAdapter;
14
15
    public function __construct(PhotoAdapter $photoAdapter)
16
    {
17
        $this->photoAdapter = $photoAdapter;
18
    }
19
20
    public function all()
21
    {
22
        $collection = [];
23
        $all = Album::all();
24
        foreach ($all as $album) {
25
            $collection[$album->id] = $this->fromEloquent($album);
26
        }
27
        return Collection::make($collection);
28
    }
29
30
    public function find($id)
31
    {
32
        $album = Album::findOrFail($id);
33
        if($album) return $this->fromEloquent($album);
34
    }
35
36
    public function findHidden($id)
37
    {
38
        $album = Album::onlyTrashed()->where('id', $id)->get();
39
        if($album) return $this->fromEloquent($album);
40
    }
41
42
    public function findByAttribute(array $attribute)
43
    {
44
        $collection = [];
45
        $all = Album::where(function($query) {
46
            foreach ($attribute as $att => $value) {
0 ignored issues
show
Bug introduced by
The variable $attribute does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
                $query->where($att, $value);
48
            }
49
        })->get();
50
        foreach ($all as $album) {
51
            $collection[$album->id] = $this->fromEloquent($album);
52
        }
53
        return Collection::make($collection)->keyBy('id');
54
    }
55
56
    public function add(Entity\Album $album)
57
    {
58
        $photos = $album->getPhotos()->toArray();
59
        foreach ($photos as $photo) {
60
            $this->photoAdapter->add($photo);
61
        }
62
        return $this->save($album);
63
    }
64
65
    public function save(Entity\Album $album)
66
    {
67
        $data = $album->toArray();
68
        if(array_key_exists('id', $data)) {
69
            return $this->update($album);
70
        } else {
71
            return $this->toEloquent($data)->save();
72
        }
73
    }
74
75
    public function update(Entity\Album $album)
76
    {
77
        $data = $album->toArray();
78
        $album = Album::find($data['id']);
79
        $album->name = $data['name'];
80
        $album->description = $data['description'];
81
        $album->order = $data['order'];
82
83
        if(isset($data['photos']))
84
        {
85
            foreach ($data['photos'] as $photo) {
86
                $this->photoAdapter->update($photo);
87
            }
88
        }
89
90
        return $album->save();
91
    }
92
93
    public function toEloquent($data)
94
    {
95
        $album = new Album;
96
        foreach ($data as $key => $value) {
97
            $album->$key = $value;
98
        }
99
        return $album;
100
    }
101
102
    public function fromEloquent(Album $album)
103
    {
104
        $entity = new Entity\Album();
105
        $entity->map([
106
            'id' => $album->id,
107
            'name' => $album->name,
108
            'description' => $album->description,
109
            'order' => $album->order,
110
            'photos' => $this->photoAdapter->findByAlbumId($album->id),
111
        ]);
112
        return $entity;
113
    }
114
115
    public function hide(Entity\Album $album)
116
    {
117
        return Album::where('id', $album->getId())->delete();
118
    }
119
    
120
    public function restore(Entity\Album $album)
121
    {
122
        return Album::withTrashed()->where('id', $album->getId())->restore();
123
    }
124
125
    public function delete(Entity\Album $album)
126
    {
127
        return Album::withTrashed()->where('id', $album->getId())->forceDelete();
128
    }
129
}