ModelAwareTrait::setModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Charcoal\Source;
4
5
use RuntimeException;
6
7
// From 'charcoal-core'
8
use Charcoal\Model\ModelInterface;
9
10
/**
11
 * Provides awareness for an object model.
12
 */
13
trait ModelAwareTrait
14
{
15
    /**
16
     * The source's object model.
17
     *
18
     * @var ModelInterface
19
     */
20
    private $model;
21
22
    /**
23
     * Set the source's model.
24
     *
25
     * @param ModelInterface $model The source's model.
26
     * @return self
27
     */
28
    public function setModel(ModelInterface $model)
29
    {
30
        $this->model = $model;
31
        return $this;
32
    }
33
34
    /**
35
     * Retrieve the source's model.
36
     *
37
     * @throws RuntimeException If not model was previously set.
38
     * @return ModelInterface
39
     */
40
    public function model()
41
    {
42
        if ($this->model === null) {
43
            throw new RuntimeException(
44
                'Model is missing for source.'
45
            );
46
        }
47
        return $this->model;
48
    }
49
50
    /**
51
     * Determine if the source has a model.
52
     *
53
     * @return boolean
54
     */
55
    public function hasModel()
56
    {
57
        return ($this->model !== null);
58
    }
59
}
60