Completed
Push — master ( 0964f5...56617a )
by Oscar
03:10
created

File::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace Folk\Entities;
4
5
use Folk\SearchQuery;
6
use SimpleCrud\Row;
7
use FilesystemIterator;
8
use RecursiveDirectoryIterator;
9
10
abstract class File extends AbstractEntity implements EntityInterface
11
{
12
    protected $extension;
13
14
    /**
15
     * Returns the base path.
16
     * 
17
     * @return string
18
     */
19
    abstract protected function getBasePath();
20
21
    protected function getIterator()
22
    {
23
        return new RecursiveDirectoryIterator($this->getBasePath(), FilesystemIterator::SKIP_DOTS);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function search(SearchQuery $search)
30
    {
31
        $result = [];
32
        $words = $search->getWords();
33
        $start = strlen($this->getBasePath()) + 1;
34
        $length = -strlen($this->extension) - 1;
35
36
        foreach ($this->getIterator() as $file) {
37
            if (!$file->isFile() || $file->getExtension() !== $this->extension) {
38
                continue;
39
            }
40
41
            $id = substr($file->getPathname(), $start, $length);
42
43
            foreach ($words as $word) {
44
                if (strpos($id, $word) === false) {
45
                    continue;
46
                }
47
            }
48
49
            $result[$id] = $this->parse(file_get_contents($file->getPathname()));
50
        }
51
52
        $sort = $search->getSort();
53
54
        if ($sort) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
            uasort($result, function ($a, $b) use ($sort) {
56
                if ($a[$sort] === $b[$sort]) {
57
                    return 0;
58
                }
59
    
60
                return ($a[$sort] < $b[$sort]) ? -1 : 1;
61
            });
62
63
            if ($search->getDirection() === 'DESC') {
64
                $result = array_reverse($result, true);
65
            }
66
        }
67
68
        if ($search->getPage() !== null) {
69
            $offset = ($search->getPage() * 50) - 50;
70
71
            $result = array_slice($result, $offset, 50, true);
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 View Code Duplication
    public function create(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $id = $this->getId($data);
83
        $file = $this->getFilePath($id);
84
        $source = $this->stringify($data);
85
86
        file_put_contents($file, $source);
87
88
        return $id;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function read($id)
95
    {
96
        $file = $this->getFilePath($id);
97
98
        return $this->parse(file_get_contents($file));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function update($id, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $file = $this->getFilePath($id);
107
        $source = $this->stringify($data);
108
109
        file_put_contents($file, $source);
110
111
        return $data;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function delete($id)
118
    {
119
        $file = $this->getFilePath($id);
120
121
        unlink($file);
122
    }
123
124
    /**
125
     * Calculate the id of a new row.
126
     * 
127
     * @param array $data
128
     * 
129
     * @return string
130
     */
131
    protected function getId(array $data)
132
    {
133
        $list = glob($this->getBasePath()."/*.{$this->extension}");
134
135
        return $list ? count($list) + 1 : uniqid();
136
    }
137
138
    /**
139
     * Returns the path of a file.
140
     * 
141
     * @return string
142
     */
143
    protected function getFilePath($filename)
144
    {
145
        return $this->getBasePath()."/{$filename}.{$this->extension}";
146
    }
147
148
    /**
149
     * Transform the data to a string.
150
     * 
151
     * @param array $data
152
     * 
153
     * @return string
154
     */
155
    abstract protected function stringify(array $data);
156
157
    /**
158
     * Transform the string to an array.
159
     * 
160
     * @param string $source
161
     * 
162
     * @return array
163
     */
164
    abstract protected function parse($source);
165
}
166