Completed
Push — master ( e4bfce...b9fe45 )
by Vladimir
02:19
created

TrackingManager::enableTracking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2016 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\Object\FrontMatterObject;
11
use allejo\stakx\System\FileExplorer;
12
use Symfony\Component\Finder\SplFileInfo;
13
14
/**
15
 * Class TrackingManager
16
 *
17
 * @package allejo\stakx\Manager
18
 */
19
abstract class TrackingManager extends BaseManager
20
{
21
    /**
22
     * @var FileExplorer
23
     */
24
    protected $fileExplorer;
25
26
    /**
27
     * An array corresponding with $folderDefinitions to store metadata regarding a specificc folder
28
     *
29
     * $folderDefinitionsOption['<folder path>'] = array()
30
     *
31
     * @var string[]
32
     */
33
    protected $folderDefinitionsOptions;
34
35
    /**
36
     * An array of folders which tracked items are stored in
37
     *
38
     * $folderDefinitions[] = '<folder path>'
39
     *
40
     * @var string[]
41
     */
42
    protected $folderDefinitions;
43
44
    /**
45
     * The storage which contains the same information as $trackedItems but organized by relative file path instead of a
46
     * namespace or file name without extension.
47
     *
48
     * $trackedItemsOptions['<relative file path>'] = mixed
49
     *
50
     * @var array
51
     */
52
    protected $trackedItemsFlattened;
53
54
    /**
55
     * The storage used to cache any information needed for a specific FrontMatterObject or DataItem.
56
     *
57
     * For example, with a DataItem, which is just an array, the file path to the original file can be stored in this
58
     * array to be accessible in the future to refresh the contents without parsing all of the files again.
59
     *
60
     * $trackedItemsOptions['<relative file path>'] = array
61
     *
62
     * @var array
63
     */
64
    protected $trackedItemsOptions;
65
66
    /**
67
     * The storage used for either FrontMatterObjects or DataItems in the respective static classes
68
     *
69
     * $trackedItems['<namespace>']['<file name w/o extension>'] = mixed
70
     * $trackedItems['<file name w/o extension>'] = mixed
71
     *
72
     * @var array
73
     */
74
    protected $trackedItems;
75
76
    /**
77
     * Set to true when file tracking is enabled
78
     *
79
     * @var bool
80
     */
81
    protected $tracking;
82
83 29
    public function __construct()
84
    {
85 29
        parent::__construct();
86
87 29
        $this->folderDefinitionsOptions = array();
88 29
        $this->folderDefinitions = array();
89 29
        $this->trackedItemsFlattened = array();
90 29
        $this->trackedItemsOptions = array();
91 29
        $this->trackedItems = array();
92 29
        $this->tracking = false;
93 29
    }
94
95
    /**
96
     * Save data to the tracker with a reference to the file it came from
97
     *
98
     * @param string      $key       The name of the file
99
     * @param mixed       $data      The data to save the
100
     * @param string      $filePath  The relative file path from the root of the website
101
     * @param string|null $namespace The name of the collection this data belongs to, if any
102
     */
103 29
    public function addArrayToTracker ($key, $data, $filePath, $namespace = null)
104
    {
105 29
        if (is_null($namespace))
106 29
        {
107
            $this->trackedItems[$key] = $data;
108
            $this->trackedItemsFlattened[$filePath] = &$this->trackedItems[$key];
109
        }
110
        else
111
        {
112 29
            $this->trackedItems[$namespace][$key] = $data;
113 29
            $this->trackedItemsFlattened[$filePath] = &$this->trackedItems[$namespace][$key];
114
        }
115 29
    }
116
117
    /**
118
     * Add a FrontMatterObject based object to the tracker
119
     *
120
     * @param FrontMatterObject $trackedItem
121
     * @param string            $key
122
     * @param string|null       $namespace
123
     */
124 29
    public function addObjectToTracker ($trackedItem, $key, $namespace = null)
125
    {
126 29
        if (!($trackedItem instanceof FrontMatterObject))
127 29
        {
128
            throw new \InvalidArgumentException('Only objects can be added to the tracker');
129
        }
130
131 29
        $this->addArrayToTracker($key, $trackedItem, $trackedItem->getRelativeFilePath(), $namespace);
132 29
    }
133
134
    /**
135
     * Remove all data related to an array that was saved
136
     *
137
     * @param string      $key
138
     * @param string      $filePath
139
     * @param string|null $namespace
140
     */
141
    public function delArrayFromTracker($key, $filePath, $namespace = null)
142
    {
143
        if (is_null($namespace))
144
        {
145
            unset($this->trackedItems[$key]);
146
        }
147
        else
148
        {
149
            unset($this->trackedItems[$namespace][$key]);
150
        }
151
152
        unset($this->trackedItemsFlattened[$filePath]);
153
    }
154
155
    /**
156
     * Remove an entry from the tracked items array
157
     *
158
     * @param mixed       $trackedItem
159
     * @param string|null $namespace
160
     */
161
    public function delObjectFromTracker ($trackedItem, $namespace = null)
162
    {
163
        $this->delArrayFromTracker(
164
            $trackedItem->getFileName(),
165
            $trackedItem->getRelativeFilePath(),
166
            $namespace
167
        );
168
    }
169
170
    /**
171
     * Whether or not to enable tracking of files.
172
     *
173
     * Setting this to false will disable a lot of the overhead and caching done when a project is being watched
174
     *
175
     * @param bool $enabled
176
     */
177
    public function enableTracking ($enabled)
178
    {
179
        $this->tracking = $enabled;
180
    }
181
182
    /**
183
     * Check to see if the file belongs inside of one the folders being tracked by this manager
184
     *
185
     * @param  string $filePath
186
     *
187
     * @return bool True if the file is inside a tracked folder
188
     */
189
    public function isHandled ($filePath)
190
    {
191
        foreach ($this->folderDefinitions as $folder)
192
        {
193
            if (substr($filePath, 0, strlen($folder)) === $folder)
194
            {
195
                return true;
196
            }
197
        }
198
199
        return false;
200
    }
201
202
    /**
203
     * Check whether a file is tracked
204
     *
205
     * @param  string $filePath The relative path of the file
206
     *
207
     * @return bool
208
     */
209 2
    public function isTracked ($filePath)
210
    {
211 2
        return array_key_exists($filePath, $this->trackedItemsFlattened);
212
    }
213
214
    /**
215
     * @param SplFileInfo|string $filePath
216
     *
217
     * @return mixed|null
218
     */
219
    public function createNewItem ($filePath)
220
    {
221
        return $this->handleTrackableItem($filePath);
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 219 can also be of type string; however, allejo\stakx\Manager\Tra...::handleTrackableItem() does only seem to accept object<Symfony\Component\Finder\SplFileInfo>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
222
    }
223
224
    /**
225
     * Update the contents of a specified file
226
     *
227
     * @param SplFileInfo|string $filePath The relative path of the file
228
     *
229
     * @return mixed|null
230
     */
231
    public function refreshItem ($filePath)
232
    {
233
        return $this->handleTrackableItem(
234
            $filePath,
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 231 can also be of type string; however, allejo\stakx\Manager\Tra...::handleTrackableItem() does only seem to accept object<Symfony\Component\Finder\SplFileInfo>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
235
            $this->trackedItemsOptions[$filePath]
236
        );
237
    }
238
239
    /**
240
     * Save a folder that is tracked by this manager and its respective options
241
     *
242
     * @param string $folderPath
243
     * @param array  $options
244
     */
245 29
    public function saveFolderDefinition ($folderPath, $options = array())
246
    {
247 29
        $this->folderDefinitions[] = $folderPath;
248 29
        $this->folderDefinitionsOptions[$folderPath] = $options;
249 29
    }
250
251
    /**
252
     * Save any options related to an item needed in order to refresh the content
253
     *
254
     * @param string $filePath
255
     * @param array $options
256
     */
257 1
    public function saveTrackerOptions ($filePath, $options = array())
258
    {
259 1
        $this->trackedItemsOptions[$filePath] = $options;
260 1
    }
261
262
    /**
263
     * Parse the specified folder for items to track
264
     *
265
     * @param string $path
266
     * @param mixed  $options  Special options that will be passed to the static::parseTrackableItem() implementation
267
     * @param array  $includes
268
     * @param array  $excludes
269
     */
270 29
    public function scanTrackableItems($path, $options = array(), $includes = array(), $excludes = array())
271
    {
272 29
        $fileExplorerFlags  = array_key_exists('fileExplorer', $options) ? $options['fileExplorer'] : null;
273 29
        $this->fileExplorer = FileExplorer::create($path, $excludes, $includes, $fileExplorerFlags);
274 29
        $fileExplorer = $this->fileExplorer->getExplorer();
275
276 29
        foreach ($fileExplorer as $file)
277
        {
278 29
            $this->handleTrackableItem($file, $options);
279 29
        }
280 29
    }
281
282
    /**
283
     * @param  SplFileInfo $filePath
284
     * @param  mixed       $options
285
     *
286
     * @return mixed|null
287
     */
288
    abstract protected function handleTrackableItem ($filePath, $options = array());
289
}