Completed
Push — master ( c2b5cc...89bfcf )
by Vladimir
03:33
created

DataManager::getDataItems()   A

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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\Exception\DependencyMissingException;
11
use allejo\stakx\Document\DataItem;
12
use allejo\stakx\Exception\UnsupportedDataTypeException;
13
use allejo\stakx\Utilities\StrUtils;
14
15
/**
16
 * Class DataManager.
17
 *
18
 * This class handles everything in regards to DataItems and DataSets. This class supports reading the following data
19
 * types:
20
 *
21
 *   - CSV
22
 *   - JSON
23
 *   - XML
24
 *   - YAML
25
 */
26
class DataManager extends TrackingManager
27
{
28
    /**
29
     * Get all of the DataItems and DataSets in this manager.
30
     *
31
     * @return array
32
     */
33 2
    public function &getDataItems()
34
    {
35 2
        return $this->trackedItems;
36
    }
37
38 2
    public function getJailedDataItems()
39
    {
40 2
        return $this->getJailedTrackedItems();
41
    }
42
43
    /**
44
     * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems.
45
     *
46
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
47
     *
48
     * @param string[] $folders An array of folders to be searched for to contain DataItems
49
     */
50 1
    public function parseDataItems($folders)
51
    {
52 1
        if ($folders === null)
53
        {
54
            return;
55
        }
56
57 1
        foreach ($folders as $folder)
58
        {
59 1
            $this->saveFolderDefinition($folder);
60 1
            $this->scanTrackableItems($folder);
61
        }
62 1
    }
63
64
    /**
65
     * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location.
66
     *
67
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
68
     *
69
     * @param string[] $dataSets An array of DataSets
70
     */
71 1
    public function parseDataSets($dataSets)
72
    {
73 1
        if ($dataSets === null)
74
        {
75
            return;
76
        }
77
78
        /**
79
         * The information which each DataSet has from the configuration file.
80
         *
81
         * $dataSet['name']   string The name of the collection
82
         *         ['folder'] string The folder where this collection has its ContentItems
83
         *
84
         * @var array
85
         */
86 1
        foreach ($dataSets as $dataSet)
87
        {
88 1
            $this->saveFolderDefinition($dataSet['folder'], array(
89 1
                'namespace' => $dataSet['name'],
90
            ));
91 1
            $this->scanTrackableItems(
92 1
                $dataSet['folder'],
93 1
                array('namespace' => $dataSet['name'])
94
            );
95
        }
96 1
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    protected function handleTrackableItem($filePath, $options = array())
102
    {
103
        try
104
        {
105 2
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
106
107 2
            $dataItem = new DataItem($filePath);
108 2
            $dataItem->setNamespace($namespace);
109
110 2
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
111 2
            $this->addObjectToTracker($dataItem, $dataItem->getName(), $namespace);
112
113 2
            return $dataItem->getName();
114
        }
115
        catch (DependencyMissingException $e)
116
        {
117
            if ($e->getDependency() === 'XML')
118
            {
119
                $this->output->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
120
                $this->output->critical('  e.g. php7.0-xml');
121
            }
122
        }
123
        catch (UnsupportedDataTypeException $e)
124
        {
125
            $this->output->warning(StrUtils::interpolate('There is no function to handle {ext} file format.', array(
126
                'ext' => $e->getDataType()
127
            )));
128
        }
129
130
        return $this->fs->getBaseName($filePath);
131
    }
132
}
133