Completed
Pull Request — master (#66)
by Vladimir
02:59
created

DataManager::parseDataItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3.0123
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\Exception\DependencyMissingException;
14
use allejo\stakx\Exception\UnsupportedDataTypeException;
15
use allejo\stakx\Filesystem\File;
16
use allejo\stakx\Filesystem\FilesystemLoader as fs;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * This class handles everything in regards to DataItems and DataSets.
22
 */
23
class DataManager extends TrackingManager
24
{
25
    private $dataTransformerManager;
26
    private $configuration;
27
    private $eventDispatcher;
28
    private $logger;
29
30
    /**
31
     * DataManager constructor.
32
     */
33 2
    public function __construct(DataTransformerManager $dataTransformerManager, Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
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...
34
    {
35 2
        $this->dataTransformerManager = $dataTransformerManager;
36 2
        $this->configuration = $configuration;
37 2
        $this->eventDispatcher = $eventDispatcher;
38 2
        $this->logger = $logger;
39 2
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function compileManager()
45
    {
46
        if (!$this->configuration->hasDataItems())
47
        {
48
            $this->logger->notice('No DataItems or Datasets detected... Ignoring.');
49
50
            return;
51
        }
52
53
        $this->parseDataItems($this->configuration->getDataFolders());
54
        $this->parseDataSets($this->configuration->getDataSets());
55
    }
56
57
    /**
58
     * Get all of the DataItems and DataSets in this manager.
59
     *
60
     * @return array
61
     */
62 2
    public function &getDataItems()
63
    {
64 2
        return $this->trackedItems;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getJailedDataItems()
71
    {
72 2
        return self::getJailedTrackedItems($this->trackedItemsFlattened, function (DataItem $dataItem) {
73 2
            return $dataItem->getBasename();
74 2
        });
75
    }
76
77
    /**
78
     * Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems.
79
     *
80
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
81
     *
82
     * @param string[] $folders An array of folders to be searched for to contain DataItems
83
     */
84 1
    public function parseDataItems($folders)
85
    {
86 1
        if ($folders === null)
87 1
        {
88
            return;
89
        }
90
91 1
        foreach ($folders as $folder)
92
        {
93 1
            $this->saveFolderDefinition($folder);
94 1
            $this->scanTrackableItems(fs::absolutePath($folder));
0 ignored issues
show
Documentation introduced by
$folder is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95 1
        }
96 1
    }
97
98
    /**
99
     * Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location.
100
     *
101
     * For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()`
102
     *
103
     * @param string[] $dataSets An array of DataSets
104
     */
105 1
    public function parseDataSets($dataSets)
106
    {
107 1
        if ($dataSets === null)
108 1
        {
109
            return;
110
        }
111
112
        /**
113
         * The information which each DataSet has from the configuration file.
114
         *
115
         * @var array $dataSet = [
116
         *   'name' => '(string) The name of the collection',
117
         *   'folder' => '(string) The folder where this collection has its ContentItems'
118
         * ]
119
         */
120 1
        foreach ($dataSets as $dataSet)
121
        {
122 1
            $this->saveFolderDefinition($dataSet['folder'], [
123 1
                'namespace' => $dataSet['name'],
124 1
            ]);
125 1
            $this->scanTrackableItems(
126 1
                fs::absolutePath($dataSet['folder']),
0 ignored issues
show
Documentation introduced by
$dataSet['folder'] is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127 1
                ['namespace' => $dataSet['name']]
128 1
            );
129 1
        }
130 1
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 2
    protected function handleTrackableItem(File $filePath, array $options = [])
136
    {
137
        try
138
        {
139 2
            $namespace = (isset($options['namespace'])) ? $options['namespace'] : null;
140
141 2
            $dataItem = new DataItem($filePath);
142 2
            $dataItem->setDataTransformer($this->dataTransformerManager);
143 2
            $dataItem->setNamespace($namespace);
144
145 2
            $this->addObjectToTracker($dataItem, $namespace);
146 2
            $this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options);
147
148 2
            return $dataItem->getRelativeFilePath();
149
        }
150
        catch (DependencyMissingException $e)
151
        {
152
            if ($e->getDependency() === 'XML')
153
            {
154
                $this->logger->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:');
155
                $this->logger->critical('  e.g. php7.0-xml');
156
            }
157
        }
158
        catch (UnsupportedDataTypeException $e)
159
        {
160
            $this->logger->warning('There is no function to handle {ext} file format.', [
161
                'ext' => $e->getDataType(),
162
            ]);
163
        }
164
165
        return $filePath->getBasename();
166
    }
167
}
168