Completed
Push — master ( 505196...f680a5 )
by Vladimir
10s
created

TrackingManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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