Completed
Pull Request — master (#11)
by Vladimir
02:33
created

TrackingManager::isTracked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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\SplFileInfo;
12
13
/**
14
 * Class TrackingManager
15
 *
16
 * @package allejo\stakx\Manager
17
 */
18
abstract class TrackingManager extends BaseManager implements Trackable
19
{
20
    /**
21
     * The storage which contains the same information as $trackedItems but organized by relative file path instead of a
22
     * namespace or file name without extension.
23
     *
24
     * $trackedItemsOptions['<relative file path>'] = mixed
25
     *
26
     * @var array
27
     */
28
    protected $trackedItemsFlattened;
29
30
    /**
31
     * The storage used to cache any information needed for a specific FrontMatterObject or DataItem.
32
     *
33
     * For example, with a DataItem, which is just an array, the file path to the original file can be stored in this
34
     * array to be accessible in the future to refresh the contents without parsing all of the files again.
35
     *
36
     * $trackedItemsOptions['<relative file path>'] = array
37
     *
38
     * @var array
39
     */
40
    protected $trackedItemsOptions;
41
42
    /**
43
     * The storage used for either FrontMatterObjects or DataItems in the respective static classes
44
     *
45
     * $trackedItems['<namespace>']['<file name w/o extension>'] = mixed
46
     * $trackedItems['<file name w/o extension>'] = mixed
47
     *
48
     * @var array
49
     */
50
    protected $trackedItems;
51
52
    /**
53
     * Set to true when file tracking is enabled
54
     *
55
     * @var bool
56
     */
57
    protected $tracking;
58
59
    public function __construct()
60
    {
61
        parent::__construct();
62
63
        $this->trackedItemsFlattened = array();
64
        $this->trackedItemsOptions = array();
65
        $this->trackedItems = array();
66
        $this->tracking = false;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function addArrayToTracker ($key, $data, $filePath, $namespace = null)
73
    {
74
        if (is_null($namespace))
75
        {
76
            $this->trackedItems[$key] = $data;
77
        }
78
        else
79
        {
80
            $this->trackedItems[$namespace][$key] = $data;
81
        }
82
83
        $this->trackedItemsFlattened[$filePath] = $data;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function addObjectToTracker (&$trackedItem, $namespace = null)
90
    {
91
        if (!($trackedItem instanceof FrontMatterObject))
92
        {
93
            throw new \InvalidArgumentException('Only objects can be added to the tracker');
94
        }
95
96
        if (is_null($namespace))
97
        {
98
            $this->trackedItems[$trackedItem->getFileName()] = &$trackedItem;
0 ignored issues
show
Bug introduced by
The method getFileName() does not seem to exist on object<allejo\stakx\Object\FrontMatterObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        }
100
        else
101
        {
102
            $this->trackedItems[$namespace][$trackedItem->getFileName()] = &$trackedItem;
0 ignored issues
show
Bug introduced by
The method getFileName() does not seem to exist on object<allejo\stakx\Object\FrontMatterObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
        }
104
105
        $this->trackedItemsFlattened[$trackedItem->getRelativeFilePath()] = &$trackedItem;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function delArrayFromTracker($key, $filePath, $namespace = null)
112
    {
113
        if (is_null($namespace))
114
        {
115
            unset($this->trackedItems[$key]);
116
        }
117
        else
118
        {
119
            unset($this->trackedItems[$namespace][$key]);
120
        }
121
122
        unset($this->trackedItemsFlattened[$filePath]);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function delObjectFromTracker ($trackedItem, $namespace = null)
129
    {
130
        $this->delArrayFromTracker(
131
            $trackedItem->getFileName(),
132
            $trackedItem->getRelativeFilePath(),
133
            $namespace
134
        );
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function enableTracking ($enabled)
141
    {
142
        $this->tracking = $enabled;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function isTracked ($filePath)
149
    {
150
        return array_key_exists($filePath, $this->trackedItemsFlattened);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function refreshItem ($filePath)
157
    {
158
        $this->handleTrackableItem(
159
            $filePath,
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 156 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...
160
            $this->trackedItemsOptions[$filePath]
161
        );
162
    }
163
164
    /**
165
     * Save any options related to an item needed in order to refresh the content
166
     *
167
     * @param string $filePath
168
     * @param array $options
169
     */
170
    public function saveOptions ($filePath, $options = array())
171
    {
172
        $this->trackedItemsOptions[$filePath] = $options;
173
    }
174
175
    /**
176
     * Parse the specified folder for items to track
177
     *
178
     * @param string $folder
179
     * @param mixed  $options  Special options that will be passed to the static::parseTrackableItem() implementation
180
     * @param array  $includes
181
     * @param array  $excludes
182
     */
183
    protected function scanTrackableItems ($folder, $options = array(), $includes = array(), $excludes = array())
184
    {
185
        $finder = $this->fs->getFinder(
186
            $includes,
187
            $excludes,
188
            $this->fs->absolutePath($folder)
189
        );
190
191
        /** @var SplFileInfo $file */
192
        foreach ($finder as $file)
193
        {
194
            $this->handleTrackableItem($file, $options);
195
        }
196
    }
197
198
    /**
199
     * @param  SplFileInfo $filePath
200
     * @param  mixed       $options
201
     *
202
     * @return mixed
203
     */
204
    abstract protected function handleTrackableItem ($filePath, $options = array());
205
}