Completed
Push — master ( 54fa46...73d26f )
by Vladimir
02:25
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 Symfony\Component\Finder\Finder;
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
     * The storage which contains the same information as $trackedItems but organized by relative file path instead of a
23
     * namespace or file name without extension.
24
     *
25
     * $trackedItemsOptions['<relative file path>'] = mixed
26
     *
27
     * @var array
28
     */
29
    protected $trackedItemsFlattened;
30
31
    /**
32
     * The storage used to cache any information needed for a specific FrontMatterObject or DataItem.
33
     *
34
     * For example, with a DataItem, which is just an array, the file path to the original file can be stored in this
35
     * array to be accessible in the future to refresh the contents without parsing all of the files again.
36
     *
37
     * $trackedItemsOptions['<relative file path>'] = array
38
     *
39
     * @var array
40
     */
41
    protected $trackedItemsOptions;
42
43
    /**
44
     * The storage used for either FrontMatterObjects or DataItems in the respective static classes
45
     *
46
     * $trackedItems['<namespace>']['<file name w/o extension>'] = mixed
47
     * $trackedItems['<file name w/o extension>'] = mixed
48
     *
49
     * @var array
50
     */
51
    protected $trackedItems;
52
53
    /**
54
     * Set to true when file tracking is enabled
55
     *
56
     * @var bool
57
     */
58
    protected $tracking;
59
60 4
    public function __construct()
61
    {
62 4
        parent::__construct();
63
64 4
        $this->trackedItemsFlattened = array();
65 4
        $this->trackedItemsOptions = array();
66 4
        $this->trackedItems = array();
67 4
        $this->tracking = false;
68 4
    }
69
70
    /**
71
     * Save data to the tracker with a reference to the file it came from
72
     *
73
     * @param string      $key       The name of the file
74
     * @param mixed       $data      The data to save the
75
     * @param string      $filePath  The relative file path from the root of the website
76
     * @param string|null $namespace The name of the collection this data belongs to, if any
77
     */
78 4
    public function addArrayToTracker ($key, $data, $filePath, $namespace = null)
79
    {
80 4
        if (is_null($namespace))
81 4
        {
82
            $this->trackedItems[$key] = $data;
83
            $this->trackedItemsFlattened[$filePath] = &$this->trackedItems[$key];
84
        }
85
        else
86
        {
87 4
            $this->trackedItems[$namespace][$key] = $data;
88 4
            $this->trackedItemsFlattened[$filePath] = &$this->trackedItems[$namespace][$key];
89
        }
90 4
    }
91
92
    /**
93
     * Add a FrontMatterObject based object to the tracker
94
     *
95
     * @param FrontMatterObject $trackedItem
96
     * @param string            $key
97
     * @param string|null       $namespace
98
     */
99 4
    public function addObjectToTracker ($trackedItem, $key, $namespace = null)
100
    {
101 4
        if (!($trackedItem instanceof FrontMatterObject))
102 4
        {
103
            throw new \InvalidArgumentException('Only objects can be added to the tracker');
104
        }
105
106 4
        $this->addArrayToTracker($key, $trackedItem, $trackedItem->getRelativeFilePath(), $namespace);
107 4
    }
108
109
    /**
110
     * Remove all data related to an array that was saved
111
     *
112
     * @param string      $key
113
     * @param string      $filePath
114
     * @param string|null $namespace
115
     */
116
    public function delArrayFromTracker($key, $filePath, $namespace = null)
117
    {
118
        if (is_null($namespace))
119
        {
120
            unset($this->trackedItems[$key]);
121
        }
122
        else
123
        {
124
            unset($this->trackedItems[$namespace][$key]);
125
        }
126
127
        unset($this->trackedItemsFlattened[$filePath]);
128
    }
129
130
    /**
131
     * Remove an entry from the tracked items array
132
     *
133
     * @param mixed       $trackedItem
134
     * @param string|null $namespace
135
     */
136
    public function delObjectFromTracker ($trackedItem, $namespace = null)
137
    {
138
        $this->delArrayFromTracker(
139
            $trackedItem->getFileName(),
140
            $trackedItem->getRelativeFilePath(),
141
            $namespace
142
        );
143
    }
144
145
    /**
146
     * Whether or not to enable tracking of files.
147
     *
148
     * Setting this to false will disable a lot of the overhead and caching done when a project is being watched
149
     *
150
     * @param bool $enabled
151
     */
152
    public function enableTracking ($enabled)
153
    {
154
        $this->tracking = $enabled;
155
    }
156
157
    /**
158
    /**
159
     * Check whether a file is tracked
160
     *
161
     * @param  string $filePath The relative path of the file
162
     *
163
     * @return bool
164
     */
165 1
    public function isTracked ($filePath)
166
    {
167 1
        return array_key_exists($filePath, $this->trackedItemsFlattened);
168
    }
169
170
    /**
171
     * Update the contents of a specified file
172
     *
173
     * @param SplFileInfo|string $filePath The relative path of the file
174
     */
175
    public function refreshItem ($filePath)
176
    {
177
        $this->handleTrackableItem(
178
            $filePath,
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 175 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...
179
            $this->trackedItemsOptions[$filePath]
180
        );
181
    }
182
183
    /**
184
     * Save any options related to an item needed in order to refresh the content
185
     *
186
     * @param string $filePath
187
     * @param array $options
188
     */
189
    public function saveTrackerOptions ($filePath, $options = array())
190
    {
191
        $this->trackedItemsOptions[$filePath] = $options;
192
    }
193
194
    /**
195
     * Parse the specified folder for items to track
196
     *
197
     * @param Finder|string $pathOrFinder
198
     * @param mixed  $options  Special options that will be passed to the static::parseTrackableItem() implementation
199
     * @param array  $includes
200
     * @param array  $excludes
201
     */
202 4
    protected function scanTrackableItems ($pathOrFinder, $options = array(), $includes = array(), $excludes = array())
203
    {
204 4
        if ($pathOrFinder instanceof Finder)
205 4
        {
206
            $finder = $pathOrFinder;
207
        }
208
        else
209
        {
210 4
            $finder = $this->fs->getFinder(
211 4
                $includes,
212 4
                $excludes,
213 4
                $this->fs->absolutePath($pathOrFinder)
214 4
            );
215
        }
216
217
        /** @var SplFileInfo $file */
218 4
        foreach ($finder as $file)
219
        {
220 4
            $this->handleTrackableItem($file, $options);
221 4
        }
222 4
    }
223
224
    /**
225
     * @param  SplFileInfo $filePath
226
     * @param  mixed       $options
227
     *
228
     * @return mixed
229
     */
230
    abstract protected function handleTrackableItem ($filePath, $options = array());
231
}