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

TrackingManager::saveOptions()   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 2
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\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
    protected $trackedItemsFlattened;
22
23
    protected $trackedItemsOptions;
24
25
    /**
26
     * $trackedItems['<collection name>']['<file name w/o extension>'] = typeof(FrontMatterObject)
27
     * $trackedItems['<file name w/o extension>'] = typeof(FrontMatterObject)
28
     *
29
     * @var array
30
     */
31
    protected $trackedItems;
32
33
    public function __construct()
34
    {
35
        parent::__construct();
36
37
        $this->trackedItemsFlattened = array();
38
        $this->trackedItems = array();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function addToTracker (&$trackedItem, $collection = null)
45
    {
46
        if (!($trackedItem instanceof FrontMatterObject))
47
        {
48
            throw new \InvalidArgumentException('Only objects can be added to the tracker');
49
        }
50
51
        if (is_null($collection))
52
        {
53
            $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...
54
        }
55
        else
56
        {
57
            $this->trackedItems[$collection][$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...
58
        }
59
60
        $this->trackedItemsFlattened[$trackedItem->getRelativeFilePath()] = &$trackedItem;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function isTracked ($filePath)
67
    {
68
        return array_key_exists($filePath, $this->trackedItemsFlattened);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function refreshItem ($filePath)
75
    {
76
        $this->parseTrackableItem(
77
            $filePath,
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by parameter $filePath on line 74 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...
78
            $this->trackedItemsOptions[$filePath]
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function saveToTracker ($key, $data, $filePath, $namespace = null)
86
    {
87
        if (is_null($namespace))
88
        {
89
            $this->trackedItems[$key] = $data;
90
        }
91
        else
92
        {
93
            $this->trackedItems[$namespace][$key] = $data;
94
        }
95
96
        $this->trackedItemsFlattened[$filePath] = $data;
97
    }
98
99
    /**
100
     * Save any options related to an item needed in order to refresh the content
101
     *
102
     * @param string $filePath
103
     * @param array $options
104
     */
105
    public function saveOptions ($filePath, $options = array())
106
    {
107
        $this->trackedItemsOptions[$filePath] = $options;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function delFromTracker ($trackedItem, $namespace = null)
114
    {
115
        if (is_null($namespace))
116
        {
117
            unset($this->trackedItems[$trackedItem->getFileName()]);
118
        }
119
        else
120
        {
121
            unset($this->trackedItems[$namespace][$trackedItem->getFileName()]);
122
        }
123
124
        unset($this->trackedItemsFlattened[$trackedItem->getRelativeFilePath()]);
125
    }
126
127
    /**
128
     * Parse the specified folder for items to track
129
     *
130
     * @param string $folder
131
     * @param mixed  $options Special options that will be passed to the static::parseTrackableItem() implementation
132
     */
133
    protected function parseTrackableItems ($folder, $options = array())
134
    {
135
        $finder = new Finder();
136
        $finder->files()
137
               ->ignoreDotFiles(true)
138
               ->ignoreUnreadableDirs()
139
               ->in($this->fs->absolutePath($folder));
140
141
        foreach ($finder as $dataItem)
142
        {
143
            $this->parseTrackableItem($dataItem, $options);
144
        }
145
    }
146
147
    /**
148
     * @param  SplFileInfo $filePath
149
     * @param  mixed       $options
150
     *
151
     * @return mixed
152
     */
153
    abstract protected function parseTrackableItem ($filePath, $options = array());
154
}