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

TrackingManager::addArrayToTracker()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
rs 9.4285
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 implements Trackable
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
    public function __construct()
61
    {
62
        parent::__construct();
63
64
        $this->trackedItemsFlattened = array();
65
        $this->trackedItemsOptions = array();
66
        $this->trackedItems = array();
67
        $this->tracking = false;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function addArrayToTracker ($key, $data, $filePath, $namespace = null)
74
    {
75
        if (is_null($namespace))
76
        {
77
            $this->trackedItems[$key] = $data;
78
        }
79
        else
80
        {
81
            $this->trackedItems[$namespace][$key] = $data;
82
        }
83
84
        $this->trackedItemsFlattened[$filePath] = $data;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function addObjectToTracker (&$trackedItem, $namespace = null)
91
    {
92
        if (!($trackedItem instanceof FrontMatterObject))
93
        {
94
            throw new \InvalidArgumentException('Only objects can be added to the tracker');
95
        }
96
97
        if (is_null($namespace))
98
        {
99
            $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...
100
        }
101
        else
102
        {
103
            $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...
104
        }
105
106
        $this->trackedItemsFlattened[$trackedItem->getRelativeFilePath()] = &$trackedItem;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function delArrayFromTracker($key, $filePath, $namespace = null)
113
    {
114
        if (is_null($namespace))
115
        {
116
            unset($this->trackedItems[$key]);
117
        }
118
        else
119
        {
120
            unset($this->trackedItems[$namespace][$key]);
121
        }
122
123
        unset($this->trackedItemsFlattened[$filePath]);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function delObjectFromTracker ($trackedItem, $namespace = null)
130
    {
131
        $this->delArrayFromTracker(
132
            $trackedItem->getFileName(),
133
            $trackedItem->getRelativeFilePath(),
134
            $namespace
135
        );
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function enableTracking ($enabled)
142
    {
143
        $this->tracking = $enabled;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function isTracked ($filePath)
150
    {
151
        return array_key_exists($filePath, $this->trackedItemsFlattened);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function refreshItem ($filePath)
158
    {
159
        $this->parseTrackableItem(
160
            $filePath,
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 157 can also be of type string; however, allejo\stakx\Manager\Tra...r::parseTrackableItem() 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...
161
            $this->trackedItemsOptions[$filePath]
162
        );
163
    }
164
165
    /**
166
     * Save any options related to an item needed in order to refresh the content
167
     *
168
     * @param string $filePath
169
     * @param array $options
170
     */
171
    public function saveOptions ($filePath, $options = array())
172
    {
173
        $this->trackedItemsOptions[$filePath] = $options;
174
    }
175
176
    /**
177
     * Parse the specified folder for items to track
178
     *
179
     * @param string $folder
180
     * @param mixed  $options Special options that will be passed to the static::parseTrackableItem() implementation
181
     */
182
    protected function parseTrackableItems ($folder, $options = array())
183
    {
184
        $finder = new Finder();
185
        $finder->files()
186
               ->ignoreDotFiles(true)
187
               ->ignoreUnreadableDirs()
188
               ->in($this->fs->absolutePath($folder));
189
190
        foreach ($finder as $dataItem)
191
        {
192
            $this->parseTrackableItem($dataItem, $options);
193
        }
194
    }
195
196
    /**
197
     * @param  SplFileInfo $filePath
198
     * @param  mixed       $options
199
     *
200
     * @return mixed
201
     */
202
    abstract protected function parseTrackableItem ($filePath, $options = array());
203
}