Completed
Push — master ( 398d0f...310d56 )
by Pascal
11:17 queued 06:22
created

AttributeModel::getIdentifier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Pbmedia\Specifications\Laravel\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Pbmedia\Specifications\Interfaces\Attribute;
7
use Pbmedia\Specifications\Laravel\Exceptions\AttributeModelNotSavedException;
8
9
class AttributeModel extends Model implements Attribute
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14
    protected $fillable = ['name'];
15
16
    /**
17
     * {@inheritDoc}
18
     */
19
    protected $table = 'specifications_attributes';
20
21
    /**
22
     * Helper method to quickly create a new AttributeModel.
23
     *
24
     * @return \Pbmedia\Specifications\Laravel\Models\AttributeModel
25
     */
26
    public static function createWithName($name)
27
    {
28
        return static::create(compact('name'));
29
    }
30
31
    /**
32
     * Returns the key by which we can identify this model.
33
     *
34
     * @throws \Pbmedia\Specifications\Laravel\Exceptions\AttributeModelNotSavedException
35
     * @return int
36
     */
37
    public function getIdentifier()
38
    {
39
        if (!$this->exists) {
40
            $exception = new AttributeModelNotSavedException;
41
            $exception->setModel($this);
42
43
            throw $exception;
44
        }
45
46
        return $this->getKey();
47
    }
48
}
49