Completed
Pull Request — master (#4)
by Mathieu
02:51
created

ModelAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setModel() 0 5 1
A model() 0 9 2
A hasModel() 0 4 1
1
<?php
2
namespace Charcoal\Source;
3
4
use Exception;
5
6
use Charcoal\Model\ModelInterface;
7
8
trait ModelAwareTrait
9
{
10
    /**
11
     * @var ModelInterface $model
12
     */
13
    private $model = null;
14
15
    /**
16
     * Set the source's Model.
17
     *
18
     * @param ModelInterface $model The source's model.
19
     * @return AbstractSource Chainable
20
     */
21
    public function setModel(ModelInterface $model)
22
    {
23
        $this->model = $model;
24
        return $this;
25
    }
26
27
    /**
28
     * Return the source's Model.
29
     *
30
     * @throws Exception If not model was previously set.
31
     * @return ModelInterface
32
     */
33
    public function model()
34
    {
35
        if ($this->model === null) {
36
            throw new Exception(
37
                'No model set.'
38
            );
39
        }
40
        return $this->model;
41
    }
42
43
    /**
44
     * @return boolean
45
     */
46
    public function hasModel()
47
    {
48
        return ($this->model !== null);
49
    }
50
}
51