MetadataLoaderAwareTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 58
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetadataLoader() 0 3 1
A metadataLoader() 0 10 2
A loadMetadata() 0 6 1
1
<?php
2
3
namespace Charcoal\Model;
4
5
use RuntimeException;
6
7
use Charcoal\Model\Service\MetadataLoader;
8
9
/**
10
 * Provides describable metadata features.
11
 *
12
 * Metadata Loader Aware Trait
13
 * @package Charcoal\Model
14
 */
15
trait MetadataLoaderAwareTrait
16
{
17
    /**
18
     * Store the metadata loader.
19
     *
20
     * @var MetadataLoader
21
     */
22
    protected $metadataLoader;
23
24
    /**
25
     * Set a metadata loader.
26
     *
27
     * @param  MetadataLoader $loader The metadata loader.
28
     * @return void
29
     */
30
    protected function setMetadataLoader(MetadataLoader $loader)
31
    {
32
        $this->metadataLoader = $loader;
33
    }
34
35
    /**
36
     * Retrieve the metadata loader.
37
     *
38
     * @throws RuntimeException If the metadata loader is missing.
39
     * @return MetadataLoader
40
     */
41
    public function metadataLoader()
42
    {
43
        if (!isset($this->metadataLoader)) {
44
            throw new RuntimeException(sprintf(
45
                'Metadata Loader is not defined for [%s]',
46
                get_class($this)
47
            ));
48
        }
49
50
        return $this->metadataLoader;
51
    }
52
53
    /**
54
     * Load a metadata file.
55
     *
56
     * @param  string $metadataIdent A metadata file path or namespace.
57
     * @return MetadataInterface
58
     */
59
    protected function loadMetadata($metadataIdent)
60
    {
61
        $metadataLoader = $this->metadataLoader();
62
        $metadata = $metadataLoader->load($metadataIdent, $this->createMetadata());
63
64
        return $metadata;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $metadata also could return the type array which is incompatible with the documented return type Charcoal\Model\MetadataInterface.
Loading history...
65
    }
66
67
    /**
68
     * Retrieve a new metadata object.
69
     *
70
     * @return MetadataInterface
71
     */
72
    abstract protected function createMetadata();
73
}
74