Completed
Push — master ( 9337f8...1eee69 )
by Patrick
03:28
created

DataSetFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 42
rs 10
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataSetByName() 0 18 3
A getDataTableByNames() 0 5 1
1
<?php
2
/**
3
 * DataSetFactory class
4
 *
5
 * This file describes the static DataSetFactory class
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * Allow other classes to be loaded as needed
16
 */
17
require_once('Autoload.php');
18
19
/**
20
 * A static class allowing the caller to easily obtain \Data\DataSet object instances
21
 *
22
 * This class will utilize the Settings class to determine who to construct the \Data\DataSet object requested by the caller
23
 */
24
class DataSetFactory
25
{
26
    /**
27
     * Obtain the \Data\DataSet given the name of the dataset used in the settings
28
     *
29
     * @param string $setName The name of the DataSet used in the Settings
30
     *
31
     * @return \Data\DataSet The DataSet specified
32
     */
33
    public static function getDataSetByName($setName)
34
    {
35
        static $instances = array();
36
        if(isset($instances[$setName]))
37
        {
38
            return $instances[$setName];
39
        }
40
        $settings = \Settings::getInstance();
41
        $setData = $settings->getDataSetData($setName);
42
        if($setData === false)
43
        {
44
            throw new Exception('Unknown dataset name '.$setName);
45
        }
46
        $class_name = '\\Data\\'.$setData['type'];
47
        $obj = new $class_name($setData['params']);
48
        $instances[$setName] = $obj;
49
        return $obj;
50
    }
51
52
    /**
53
     * Obtain the \Data\DataTable given the name of the dataset used in the settings and the name of the table
54
     *
55
     * @param string $dataSetName The name of the DataSet used in the Settings
56
     * @param string $dataTableName The name of the DataTable
57
     *
58
     * @return \Data\DataTable The DataTable specified
59
     */
60
    public static function getDataTableByNames($dataSetName, $dataTableName)
61
    {
62
        $dataSet = self::getDataSetByName($dataSetName);
63
        return $dataSet[$dataTableName];
64
    }
65
}
66
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
67