FileCollection::del()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace AppBundle\Sync\Entity;
3
4
use AppBundle\Exception\FilterException;
5
use AppBundle\Sync\Entity\Filter\FilterInterface;
6
7
/**
8
 * Collection of Files
9
 *
10
 * @author Sergey Sadovoi <[email protected]>
11
 */
12
class FileCollection extends Collection
13
{
14
    /**
15
     * @var array  Index of files by uid
16
     */
17
    protected $index = [];
18
19
    /**
20
     * Adds file to the collection
21
     *
22
     * @param File $file
23
     */
24 9
    public function addFile(File $file)
25
    {
26 9
        $this->add($file, $file->getPath());
27 9
    }
28
29
    /**
30
     * Filter the collection
31
     *
32
     * @param array $filters Array of Filter objects
33
     *
34
     * @throws FilterException
35
     */
36 4
    public function filter(array $filters)
37
    {
38 4
        foreach ($this->items as $key => $item) {
39 4
            foreach ($filters as $filter) {
40 4
                if (!($filter instanceof FilterInterface)) {
41 2
                    $type = gettype($filter);
42 2
                    if ($type == 'object') {
43 1
                        $type = get_class($filter);
44 1
                    }
45
46 2
                    throw new FilterException(sprintf('Filter must be an instance of Filter class, %s given', $type));
47
                }
48
49 2
                if (!$filter->valid($item)) {
50 1
                    $this->del($key);
51 1
                    break;
52
                }
53 2
            }
54 2
        }
55 2
    }
56
57
    /**
58
     * Add file and update index
59
     *
60
     * @param File $file
61
     * @param string $key
62
     */
63 9
    public function add($file, $key = null)
64
    {
65 9
        $this->addToIndex($file);
66
67 9
        parent::add($file, $key);
68 9
    }
69
70
    /**
71
     * Del file and update index
72
     *
73
     * @param string $key  File key
74
     */
75 2
    public function del($key)
76
    {
77 2
        $file = $this->get($key);
78 2
        if (!is_null($file)) {
79 2
            $this->deleteFromIndex($file);
80 2
        }
81
82 2
        parent::del($key);
83 2
    }
84
85
    /**
86
     * Search the file using index
87
     *
88
     * @param string $uid  Unique id of file
89
     *
90
     * @return null|File
91
     */
92 4
    public function getByUid($uid)
93
    {
94 4
        if (!isset($this->index[$uid])) {
95 3
            return null;
96
        }
97
98 4
        $files = $this->index[$uid];
99 4
        if (count($files) == 0) {
100
            return null;
101
        }
102
103 4
        $key = array_pop($files);
104
105 4
        return $this->get($key);
106
    }
107
108
    /**
109
     * Add file to index for searching
110
     *
111
     * @param File $file
112
     */
113 9
    protected function addToIndex(File $file)
114
    {
115 9
        $uid = $file->getUid();
116
117 9
        if (isset($this->index[$uid])) {
118 6
            foreach ($this->index[$uid] as $path) {
119 6
                if ($file->getPath() == $path) {
120 2
                    return;
121
                }
122 5
            }
123 5
        }
124
125 9
        $this->index[$uid][] = $file->getPath();
126 9
    }
127
128
    /**
129
     * Delete file from index
130
     *
131
     * @param File $file
132
     *
133
     * @return bool
134
     */
135 2
    protected function deleteFromIndex(File $file)
136
    {
137 2
        if (!isset($this->index[$file->getUid()])) {
138
            return true;
139
        }
140
141
        // Remove element from index
142 2
        $index =& $this->index[$file->getUid()];
143 2
        foreach ($index as $key => $path) {
144 2
            if ($path == $file->getPath()) {
145 2
                unset($index[$key]);
146 2
            }
147 2
        }
148
149
        // Remove empty index
150 2
        if (count($index) == 0) {
151 2
            unset($this->index[$file->getUid()]);
152 2
        }
153
154 2
        return true;
155
    }
156
}
157