Completed
Pull Request — master (#48)
by Vladimir
02:44
created

DataManager::fromCsv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 9.4285
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
    public function &getDataItems()
34
    {
35
        return $this->trackedItems;
36
    }
37
38
    /**
39
     * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems.
40
     *
41
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
42
     *
43
     * @param string[] $folders An array of folders to be searched for to contain DataItems
44
     */
45
    public function parseDataItems($folders)
46
    {
47
        if ($folders === null)
48
        {
49
            return;
50
        }
51
52
        foreach ($folders as $folder)
53
        {
54
            $this->saveFolderDefinition($folder);
55
            $this->scanTrackableItems(
56
                $folder,
57
                array(),
58
                array(),
59
                array('/\.example$/')
60
            );
61
        }
62
    }
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
    public function parseDataSets($dataSets)
72
    {
73
        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
        foreach ($dataSets as $dataSet)
87
        {
88
            $this->saveFolderDefinition($dataSet['folder'], array(
89
                'namespace' => $dataSet['name'],
90
            ));
91
            $this->scanTrackableItems(
92
                $dataSet['folder'],
93
                array('namespace' => $dataSet['name']),
94
                array(),
95
                array('/\.example$/')
96
            );
97
        }
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function handleTrackableItem($filePath, $options = array())
104
    {
105
        try
106
        {
107
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
108
109
            $dataItem = new DataItem($filePath);
110
            $dataItem->setNamespace($namespace);
111
112
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
113
            $this->addObjectToTracker($dataItem, $dataItem->getName(), $namespace);
114
115
            return $dataItem->getName();
116
        }
117
        catch (DependencyMissingException $e)
118
        {
119
            if ($e->getDependency() === 'XML')
120
            {
121
                $this->output->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
122
                $this->output->critical('  e.g. php7.0-xml');
123
            }
124
        }
125
        catch (UnsupportedDataTypeException $e)
126
        {
127
            $this->output->warning(StrUtils::interpolate('There is no function to handle {ext} file format.', array(
128
                'ext' => $e->getDataType()
129
            )));
130
        }
131
132
        return $this->fs->getBaseName($filePath);
133
    }
134
}
135