Completed
Push — master ( 1e85f9...30a20e )
by Mustafa Hussain
02:04
created

BaseRepository::findWhere()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mustafa
5
 * Date: 27/04/17
6
 * Time: 06:57 م
7
 */
8
9
namespace GuardiansLabs\Repository\Repositories;
10
11
use GuardiansLabs\Repository\Contracts\RepositoryContract;
12
use GuardiansLabs\Repository\Exceptions\RepositoryException;
13
use Illuminate\Database\Eloquent\Model;
14
use Illuminate\Support\Collection;
15
16
class BaseRepository implements RepositoryContract
17
{
18
19
    protected $model;
20
    /**
21
     * @param Model $model
22
     * @return object
23
     */
24
    public function setModel(Model $model)
25
    {
26
        $this->model = $model;
27
        return $this;
28
    }
29
30
    /**
31
     * @return Model
32
     */
33
    public function getModel()
34
    {
35
        return $this->model;
36
    }
37
38
    /**
39
     * @return Collection
40
     */
41
    public function findAll()
42
    {
43
        return $this->model->get();
44
    }
45
46
    /**
47
     * @param array $data
48
     * @return Collection
49
     * @throws RepositoryException
50
     */
51
    public function createNew(array $data)
52
    {
53
        if (empty($data)) {
54
            throw new RepositoryException("No Given Data To Insert");
55
        }
56
        $this->model->fill($data);
57
        return $this->model->save();
58
    }
59
60
    /**
61
     * @param $itemId
62
     * @throws RepositoryException
63
     * @return Collection
64
     */
65
    public function findItemById($itemId)
66
    {
67
        $item = $this->model->find($itemId);
68
        if (!$item) {
69
            throw new RepositoryException("Item Not Found");
70
        }
71
        return $item;
72
    }
73
74
    /**
75
     * @param $itemId
76
     * @param array $data
77
     * @throws RepositoryException
78
     * @return mixed
79
     */
80
    public function update($itemId, array $data)
81
    {
82
        $item = $this->model->find($itemId);
83
        if (empty($data)) {
84
            throw new RepositoryException("No Given Data To Update");
85
        }
86
        if (!$item) {
87
            throw new RepositoryException("No Item Found To Update");
88
        }
89
        $item->fill($data);
90
        return $item->save();
91
    }
92
93
    /**
94
     * @param $itemId
95
     * @return mixed
96
     * @throws RepositoryException
97
     */
98
    public function delete($itemId)
99
    {
100
        $item = $this->model->find($itemId);
101
        if (!$item) {
102
            throw new RepositoryException("No Item Found To delete");
103
        }
104
        return $item->delete();
105
    }
106
}
107