Completed
Push — develop ( 667cd2...043012 )
by Vladimir
01:48
created

DataManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/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\Document\DataItem;
13
use allejo\stakx\Event\DataItemAdded;
14
use allejo\stakx\Event\DataItemFolderAdded;
15
use allejo\stakx\Event\DatasetDefinitionAdded;
16
use allejo\stakx\Exception\DependencyMissingException;
17
use allejo\stakx\Exception\UnsupportedDataTypeException;
18
use allejo\stakx\Filesystem\File;
19
use allejo\stakx\Filesystem\FileExplorerDefinition;
20
use allejo\stakx\Filesystem\Folder;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24
/**
25
 * This class handles everything in regards to DataItems and DataSets.
26
 */
27
class DataManager extends TrackingManager
28
{
29
    private $dataTransformerManager;
30
    private $configuration;
31
    private $eventDispatcher;
32
    private $logger;
33
34
    /**
35
     * DataManager constructor.
36
     */
37 2
    public function __construct(DataTransformerManager $dataTransformerManager, Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
38
    {
39 2
        $this->dataTransformerManager = $dataTransformerManager;
40 2
        $this->configuration = $configuration;
41 2
        $this->eventDispatcher = $eventDispatcher;
42 2
        $this->logger = $logger;
43 2
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function compileManager()
49
    {
50
        if (!$this->configuration->hasDataItems())
51
        {
52
            $this->logger->notice('No DataItems or Datasets detected... Ignoring.');
53
54
            return;
55
        }
56
57
        $this->parseDataItems($this->configuration->getDataFolders());
58
        $this->parseDataSets($this->configuration->getDataSets());
59
    }
60
61
    /**
62
     * Get all of the DataItems and DataSets in this manager.
63
     *
64
     * @return array
65
     */
66 2
    public function &getDataItems()
67
    {
68 2
        return $this->trackedItems;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getJailedDataItems()
75
    {
76 2
        return self::getJailedTrackedItems($this->trackedItemsFlattened, function (DataItem $dataItem) {
77 2
            return $dataItem->getBasename();
78 2
        });
79
    }
80
81
    /**
82
     * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems.
83
     *
84
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
85
     *
86
     * @param string[] $folders An array of folders to be searched for to contain DataItems
87
     */
88 1
    public function parseDataItems($folders)
89
    {
90 1
        if ($folders === null)
91
        {
92
            return;
93
        }
94
95 1
        foreach ($folders as $folder)
96
        {
97 1
            $cls = new Folder($folder);
98
99 1
            $this->logger->debug('Scanning "{folder}" for data items...', [
100 1
                'folder' => $cls->getRelativeFilePath(),
101
            ]);
102
103 1
            $event = new DataItemFolderAdded($cls);
104 1
            $this->eventDispatcher->dispatch(DataItemFolderAdded::NAME, $event);
105
106 1
            $def = new FileExplorerDefinition($cls);
107 1
            $this->scanTrackableItems($def);
108
        }
109 1
    }
110
111
    /**
112
     * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location.
113
     *
114
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
115
     *
116
     * @param string[] $dataSets An array of DataSets
117
     */
118 1
    public function parseDataSets($dataSets)
119
    {
120 1
        if ($dataSets === null)
121
        {
122
            return;
123
        }
124
125
        /**
126
         * The information which each DataSet has from the configuration file.
127
         *
128
         * @var array $dataSet = [
129
         *   'name' => '(string) The name of the collection',
130
         *   'folder' => '(string) The folder where this collection has its ContentItems'
131
         * ]
132
         */
133 1
        foreach ($dataSets as $dataSet)
134
        {
135 1
            $folder = new Folder($dataSet['folder']);
136
137 1
            $this->logger->debug('Scanning "{folder}" for the "{name}" dataset...', [
138 1
                'folder' => $folder->getRelativeFilePath(),
139 1
                'name' => $dataSet['name'],
140
            ]);
141
142 1
            $event = new DatasetDefinitionAdded($dataSet['name'], $folder);
143 1
            $this->eventDispatcher->dispatch(DatasetDefinitionAdded::NAME, $event);
144
145 1
            $def = new FileExplorerDefinition($folder);
146 1
            $this->declareTrackingNamespace($dataSet['name']);
147 1
            $this->scanTrackableItems($def, [
148 1
                'namespace' => $dataSet['name'],
149
            ]);
150
        }
151 1
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 2
    protected function handleTrackableItem(File $filePath, array $options = [])
157
    {
158
        try
159
        {
160 2
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
161
162 2
            $dataItem = new DataItem($filePath);
163 2
            $dataItem->setDataTransformer($this->dataTransformerManager);
164 2
            $dataItem->setNamespace($namespace);
165
166 2
            $event = new DataItemAdded($dataItem);
167 2
            $this->eventDispatcher->dispatch(DataItemAdded::NAME, $event);
168
169 2
            $this->addObjectToTracker($dataItem, $namespace);
170 2
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
171
172 2
            return $dataItem->getRelativeFilePath();
173
        }
174
        catch (DependencyMissingException $e)
175
        {
176
            if ($e->getDependency() === 'XML')
177
            {
178
                $this->logger->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
179
                $this->logger->critical('  e.g. php7.0-xml');
180
            }
181
        }
182
        catch (UnsupportedDataTypeException $e)
183
        {
184
            $this->logger->warning('There is no function to handle {ext} file format.', [
185
                'ext' => $e->getDataType(),
186
            ]);
187
        }
188
189
        return $filePath->getBasename();
190
    }
191
}
192