ModelFactoryTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 36
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setModelFactory() 0 3 1
A modelFactory() 0 10 2
1
<?php
2
3
namespace Charcoal\Model;
4
5
use RuntimeException;
6
7
// from `charcoal-factory`
8
use Charcoal\Factory\FactoryInterface;
9
10
/**
11
 * Provides model loader builder features.
12
 *
13
 * Trait ModelLoaderAwareTrait
14
 * @package Charcoal\Model
15
 */
16
trait ModelFactoryTrait
17
{
18
    /**
19
     * Store the factory instance.
20
     *
21
     * @var FactoryInterface
22
     */
23
    protected $modelFactory;
24
25
    /**
26
     * Set an model factory.
27
     *
28
     * @param  FactoryInterface $factory The factory to create models.
29
     * @return void
30
     */
31
    protected function setModelFactory(FactoryInterface $factory)
32
    {
33
        $this->modelFactory = $factory;
34
    }
35
36
    /**
37
     * Retrieve the model factory.
38
     *
39
     * @throws RuntimeException If the model factory is missing.
40
     * @return FactoryInterface
41
     */
42
    public function modelFactory()
43
    {
44
        if (!isset($this->modelFactory)) {
45
            throw new RuntimeException(sprintf(
46
                'Model Factory is not defined for [%s]',
47
                get_class($this)
48
            ));
49
        }
50
51
        return $this->modelFactory;
52
    }
53
}
54