Completed
Push — master ( 5e98ae...0ba1b3 )
by Vladimir
02:43
created

DataManager::handleTrackableItem()   B

Complexity

Conditions 5
Paths 38

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.7918

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 7
cts 15
cp 0.4667
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 38
nop 2
crap 8.7918
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(
61
                $folder,
62 1
                array(),
63 1
                array(),
64 1
                array('/\.example$/')
65
            );
66
        }
67 1
    }
68
69
    /**
70
     * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location.
71
     *
72
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
73
     *
74
     * @param string[] $dataSets An array of DataSets
75
     */
76 1
    public function parseDataSets($dataSets)
77
    {
78 1
        if ($dataSets === null)
79
        {
80
            return;
81
        }
82
83
        /**
84
         * The information which each DataSet has from the configuration file.
85
         *
86
         * $dataSet['name']   string The name of the collection
87
         *         ['folder'] string The folder where this collection has its ContentItems
88
         *
89
         * @var array
90
         */
91 1
        foreach ($dataSets as $dataSet)
92
        {
93 1
            $this->saveFolderDefinition($dataSet['folder'], array(
94 1
                'namespace' => $dataSet['name'],
95
            ));
96 1
            $this->scanTrackableItems(
97 1
                $dataSet['folder'],
98 1
                array('namespace' => $dataSet['name']),
99 1
                array(),
100 1
                array('/\.example$/')
101
            );
102
        }
103 1
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    protected function handleTrackableItem($filePath, $options = array())
109
    {
110
        try
111
        {
112 2
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
113
114 2
            $dataItem = new DataItem($filePath);
115 2
            $dataItem->setNamespace($namespace);
116
117 2
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
118 2
            $this->addObjectToTracker($dataItem, $dataItem->getName(), $namespace);
119
120 2
            return $dataItem->getName();
121
        }
122
        catch (DependencyMissingException $e)
123
        {
124
            if ($e->getDependency() === 'XML')
125
            {
126
                $this->output->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
127
                $this->output->critical('  e.g. php7.0-xml');
128
            }
129
        }
130
        catch (UnsupportedDataTypeException $e)
131
        {
132
            $this->output->warning(StrUtils::interpolate('There is no function to handle {ext} file format.', array(
133
                'ext' => $e->getDataType()
134
            )));
135
        }
136
137
        return $this->fs->getBaseName($filePath);
138
    }
139
}
140