Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

DataManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
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\Configuration;
11
use allejo\stakx\DataTransformer\DataTransformerManager;
12
use allejo\stakx\Exception\DependencyMissingException;
13
use allejo\stakx\Document\DataItem;
14
use allejo\stakx\Exception\UnsupportedDataTypeException;
15
use allejo\stakx\Utilities\StrUtils;
16
17
/**
18
 * This class handles everything in regards to DataItems and DataSets.
19
 */
20
class DataManager extends TrackingManager
21
{
22
    private $dataTransformerManager;
23
    private $configuration;
24
25
    /**
26
     * DataManager constructor.
27
     */
28 2
    public function __construct(DataTransformerManager $dataTransformerManager, Configuration $configuration)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $dataTransformerManager exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
29
    {
30 2
        parent::__construct();
31
32 2
        $this->dataTransformerManager = $dataTransformerManager;
33 2
        $this->configuration = $configuration;
34 2
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function compileManager()
40
    {
41
        if (!$this->configuration->hasDataItems())
42
        {
43
            $this->output->notice('No DataItems or Datasets detected... Ignoring.');
44
            return;
45
        }
46
47
        $this->parseDataItems($this->configuration->getDataFolders());
48
        $this->parseDataSets($this->configuration->getDataSets());
49
    }
50
51
    /**
52
     * Get all of the DataItems and DataSets in this manager.
53
     *
54
     * @return array
55
     */
56 2
    public function &getDataItems()
57
    {
58 2
        return $this->trackedItems;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getJailedDataItems()
65
    {
66 2
        return self::getJailedTrackedItems($this->trackedItemsFlattened, function (DataItem $dataItem) {
67 2
            return $dataItem->getBasename();
68 2
        });
69
    }
70
71
    /**
72
     * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems.
73
     *
74
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
75
     *
76
     * @param string[] $folders An array of folders to be searched for to contain DataItems
77
     */
78 1
    public function parseDataItems($folders)
79
    {
80 1
        if ($folders === null)
81 1
        {
82
            return;
83
        }
84
85 1
        foreach ($folders as $folder)
86
        {
87 1
            $this->saveFolderDefinition($folder);
88 1
            $this->scanTrackableItems($folder);
89 1
        }
90 1
    }
91
92
    /**
93
     * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location.
94
     *
95
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
96
     *
97
     * @param string[] $dataSets An array of DataSets
98
     */
99 1
    public function parseDataSets($dataSets)
100
    {
101 1
        if ($dataSets === null)
102 1
        {
103
            return;
104
        }
105
106
        /**
107
         * The information which each DataSet has from the configuration file.
108
         *
109
         * $dataSet['name']   string The name of the collection
110
         *         ['folder'] string The folder where this collection has its ContentItems
111
         *
112
         * @var array $dataSet
113
         */
114 1
        foreach ($dataSets as $dataSet)
115
        {
116 1
            $this->saveFolderDefinition($dataSet['folder'], array(
117 1
                'namespace' => $dataSet['name'],
118 1
            ));
119 1
            $this->scanTrackableItems(
120 1
                $dataSet['folder'],
121 1
                array('namespace' => $dataSet['name'])
122 1
            );
123 1
        }
124 1
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2
    protected function handleTrackableItem($filePath, array $options = array())
130
    {
131
        try
132
        {
133 2
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
134
135 2
            $dataItem = new DataItem($filePath);
136 2
            $dataItem->setDataTransformer($this->dataTransformerManager);
137 2
            $dataItem->setNamespace($namespace);
138
139 2
            $this->addObjectToTracker($dataItem, $namespace);
140 2
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
141
142 2
            return $dataItem->getRelativeFilePath();
143
        }
144
        catch (DependencyMissingException $e)
145
        {
146
            if ($e->getDependency() === 'XML')
147
            {
148
                $this->output->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
149
                $this->output->critical('  e.g. php7.0-xml');
150
            }
151
        }
152
        catch (UnsupportedDataTypeException $e)
153
        {
154
            $this->output->warning('There is no function to handle {ext} file format.', [
155
                'ext' => $e->getDataType(),
156
            ]);
157
        }
158
159
        return $this->fs->getBaseName($filePath);
160
    }
161
}
162