Test Failed
Push — master ( 55431f...64d5a8 )
by Jeff
04:55
created

AttributeRepository::associateAttributeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Shop\Attributes\Repositories;
4
5
use App\Shop\Attributes\Attribute;
6
use App\Shop\Attributes\Exceptions\AttributeNotFoundException;
7
use App\Shop\Attributes\Exceptions\CreateAttributeErrorException;
8
use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException;
9
use App\Shop\AttributeValues\AttributeValue;
10
use App\Shop\Base\BaseRepository;
11
use Illuminate\Database\Eloquent\ModelNotFoundException;
12
use Illuminate\Support\Collection;
13
use Illuminate\Database\QueryException;
14
15
class AttributeRepository extends BaseRepository implements AttributeRepositoryInterface
16
{
17
    /**
18
     * @var Attribute
19
     */
20
    protected $model;
21
22
    /**
23
     * AttributeRepository constructor.
24
     * @param Attribute $attribute
25
     */
26
    public function __construct(Attribute $attribute)
27
    {
28
        parent::__construct($attribute);
29
        $this->model = $attribute;
30
    }
31
32
    /**
33
     * @param array $data
34
     * @return Attribute
35
     * @throws CreateAttributeErrorException
36
     */
37
    public function createAttribute(array $data) : Attribute
38
    {
39
        try {
40
            $attribute = new Attribute($data);
41
            $attribute->save();
42
            return $attribute;
43
        } catch (QueryException $e) {
44
            throw new CreateAttributeErrorException($e);
45
        }
46
    }
47
48
    /**
49
     * @param int $id
50
     * @return Attribute
51
     * @throws AttributeNotFoundException
52
     */
53
    public function findAttributeById(int $id) : Attribute
54
    {
55
        try {
56
            return $this->findOneOrFail($id);
57
        } catch (ModelNotFoundException $e) {
58
            throw new AttributeNotFoundException($e);
59
        }
60
    }
61
62
    /**
63
     * @param array $data
64
     * @return bool
65
     * @throws UpdateAttributeErrorException
66
     */
67
    public function updateAttribute(array $data) : bool
68
    {
69
        try {
70
            return $this->model->update($data);
71
        } catch (QueryException $e) {
72
            throw new UpdateAttributeErrorException($e);
73
        }
74
    }
75
76
    /**
77
     * @return bool|null
78
     */
79
    public function deleteAttribute() : ?bool
80
    {
81
        return $this->model->delete();
82
    }
83
84
    /**
85
     * @param array $columns
86
     * @param string $orderBy
87
     * @param string $sortBy
88
     * @return Collection
89
     */
90
    public function listAttributes($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') : Collection
91
    {
92
        return $this->all($columns, $orderBy, $sortBy);
93
    }
94
95
    /**
96
     * @return Collection
97
     */
98
    public function listAttributeValues() : Collection
99
    {
100
        return $this->model->values()->get();
101
    }
102
103
    /**
104
     * @param AttributeValue $attributeValue
105
     * @return AttributeValue
106
     */
107
    public function associateAttributeValue(AttributeValue $attributeValue) : AttributeValue
108
    {
109
        return $this->model->values()->save($attributeValue);
110
    }
111
}
112