ModelLoaderBuilderTrait::setModelLoaderBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Charcoal\Model;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
use Charcoal\Model\Service\ModelLoader;
9
use Charcoal\Model\Service\ModelLoaderBuilder;
10
11
/**
12
 *
13
 * Provides model loader builder features.
14
 *
15
 * Model Loader Builder Trait
16
 * @package Charcoal\Model
17
 */
18
trait ModelLoaderBuilderTrait
19
{
20
    /**
21
     * Store the builder instance.
22
     *
23
     * @var ModelLoaderBuilder
24
     */
25
    protected $modelLoaderBuilder;
26
27
    /**
28
     * Store all model loaders.
29
     *
30
     * @var ModelLoader[]
31
     */
32
    private static $modelLoaders = [];
33
34
    /**
35
     * Set an model loader builder.
36
     *
37
     * @param  ModelLoaderBuilder $builder The builder to create models.
38
     * @return void
39
     */
40
    protected function setModelLoaderBuilder(ModelLoaderBuilder $builder)
41
    {
42
        $this->modelLoaderBuilder = $builder;
43
    }
44
45
    /**
46
     * Retrieve the model loader builder.
47
     *
48
     * @throws RuntimeException If the model loader builder is missing.
49
     * @return ModelLoaderBuilder
50
     */
51
    protected function modelLoaderBuilder()
52
    {
53
        if (!isset($this->modelLoaderBuilder)) {
54
            throw new RuntimeException(sprintf(
55
                'Model Factory is not defined for [%s]',
56
                get_class($this)
57
            ));
58
        }
59
60
        return $this->modelLoaderBuilder;
61
    }
62
63
    /**
64
     * Retrieve the object loader for the given model.
65
     *
66
     * @param  string      $objType The target model.
67
     * @param  string|null $objKey  The target model's key to load by.
68
     * @throws InvalidArgumentException If the $objType or $objKey are invalid.
69
     * @return ModelInterface
70
     */
71
    protected function modelLoader($objType, $objKey = null)
72
    {
73
        if (!is_string($objType)) {
0 ignored issues
show
introduced by
The condition is_string($objType) is always true.
Loading history...
74
            throw new InvalidArgumentException(sprintf(
75
                'The object type must be a string, received %s',
76
                is_object($objType) ? get_class($objType) : gettype($objType)
77
            ));
78
        }
79
80
        $key = $objKey;
81
        if ($key === null) {
82
            $key = '_';
83
        } elseif (!is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
84
            throw new InvalidArgumentException(sprintf(
85
                'The object property key must be a string, received %s',
86
                is_object($key) ? get_class($key) : gettype($key)
87
            ));
88
        }
89
90
        if (isset(self::$modelLoaders[$objType][$key])) {
91
            return self::$modelLoaders[$objType][$key];
92
        }
93
94
        $builder = $this->modelLoaderBuilder();
95
96
        self::$modelLoaders[$objType][$key] = $builder($objType, $objKey);
97
98
        return self::$modelLoaders[$objType][$key];
99
    }
100
}
101