collectionLoaderFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Charcoal\Loader;
4
5
use RuntimeException;
6
use Charcoal\Factory\FactoryInterface;
7
8
/**
9
 * Provides factoring for collection loader.
10
 *
11
 * Collection Loader Factory Trait
12
 * @package Charcoal\Loader
13
 */
14
trait CollectionLoaderFactoryTrait
15
{
16
    /**
17
     * Store the factory instance.
18
     *
19
     * @var FactoryInterface
20
     */
21
    protected $collectionLoaderFactory;
22
23
    /**
24
     * Set a model collection loader factory.
25
     *
26
     * @param  FactoryInterface $factory The factory to create model collection loaders.
27
     * @return void
28
     */
29
    protected function setCollectionLoaderFactory(FactoryInterface $factory)
30
    {
31
        $this->collectionLoaderFactory = $factory;
32
    }
33
34
    /**
35
     * Retrieve the collection loader factory.
36
     *
37
     * @throws RuntimeException If the collection loader factory is missing.
38
     * @return FactoryInterface
39
     */
40
    public function collectionLoaderFactory()
41
    {
42
        if (!isset($this->collectionLoaderFactory)) {
43
            throw new RuntimeException(sprintf(
44
                'Collection Loader Factory is not defined for [%s]',
45
                get_class($this)
46
            ));
47
        }
48
49
        return $this->collectionLoaderFactory;
50
    }
51
52
    /**
53
     * Create a model collection loader with optional constructor arguments and a post-creation callback.
54
     *
55
     * @param  array|null    $args     Optional. Constructor arguments.
56
     * @param  callable|null $callback Optional. Called at creation.
57
     * @return CollectionLoader
58
     */
59
    public function createCollectionLoaderWith(array $args = null, callable $callback = null)
60
    {
61
        $factory = $this->collectionLoaderFactory();
62
63
        return $factory->create($factory->defaultClass(), $args, $callback);
64
    }
65
66
    /**
67
     * Create a model collection loader.
68
     *
69
     * @return CollectionLoader
70
     */
71
    public function createCollectionLoader()
72
    {
73
        $factory = $this->collectionLoaderFactory();
74
75
        return $factory->create($factory->defaultClass());
76
    }
77
}
78