Metadata::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Drest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Lee Davis
9
 * @copyright Copyright (c) Lee Davis <@leedavis81>
10
 * @link https://github.com/leedavis81/drest/blob/master/LICENSE
11
 * @license http://opensource.org/licenses/MIT The MIT X License (MIT)
12
 */
13
namespace Drest\Manager;
14
15
use Drest\Mapping\MetadataFactory;
16
use Drest\Configuration;
17
use Drest\Router;
18
19
class Metadata
20
{
21
22
    /**
23
     * Metadata factory object
24
     * @var MetadataFactory $metadataFactory
25
     */
26
    protected $metadataFactory;
27
28
    /**
29
     * Mapping Driver being used
30
     * @var \Drest\Mapping\Driver\DriverInterface $metaDataDriver
31
     */
32
    protected $metaDataDriver;
33
34
    /**
35
     * Create a metadata manager instance
36
     * @param Configuration $config
37
     */
38 31
    public function __construct(Configuration $config)
39
    {
40 31
        $driver = $config->getMetadataDriverClass();
41
42 31
        $this->metaDataDriver = $driver::create(
43 31
            $config->getPathsToConfigFiles()
44 31
        );
45
46 31
        $this->metadataFactory = new MetadataFactory(
47 31
            $this->metaDataDriver
48 31
        );
49
50 31
        if ($cache = $config->getMetadataCacheImpl()) {
51 31
            $this->metadataFactory->setCache($cache);
52 31
        }
53 31
    }
54
55
    /**
56
     * Static call to create a representation instance
57
     * @param Configuration $config
58
     * @return MetaData
59
     */
60 31
    public static function create(Configuration &$config)
61
    {
62 31
        return new self($config);
63
    }
64
65
66
    /**
67
     * Get all the class names
68
     * @return array
69
     */
70 1
    public function getAllClassNames()
71
    {
72 1
        return $this->metadataFactory->getAllClassNames();
73
    }
74
75
    /**
76
     * Get the metadata for a class
77
     * @param $class
78
     * @throws \Drest\DrestException
79
     * @return \Drest\Mapping\ClassMetaData
80
     */
81 31
    public function getMetaDataForClass($class)
82
    {
83 31
        return $this->metadataFactory->getMetadataForClass($class);
84
    }
85
86
    /**
87
     * Read any defined route patterns from metadata and inject them into the router
88
     * @param Router $router
89
     */
90 31
    public function registerRoutes(Router $router)
91
    {
92 31
        foreach ($this->metaDataDriver->getAllClassNames() as $class)
93
        {
94 31
            $classMetaData = $this->getMetaDataForClass($class);
95 31
            $router->registerRoutes($classMetaData->getRoutesMetaData());
96 31
        }
97 31
    }
98
}
99