Completed
Push — master ( a7c303...506ee6 )
by Oscar
03:25
created

FileEntity::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 FileEntity 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 = null)
30
    {
31
        $result = [];
32
        $words = $search->getWords();
0 ignored issues
show
Bug introduced by
It seems like $search is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
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
        return $result;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 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...
59
    {
60
        $file = $this->getFilePath($this->getId($data));
61
        $source = $this->stringify($data);
62
63
        file_put_contents($file, $source);
64
65
        return $file;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function read($id)
72
    {
73
        $file = $this->getFilePath($id);
74
75
        return $this->parse(file_get_contents($file));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 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...
82
    {
83
        $file = $this->getFilePath($id);
84
        $source = $this->stringify($data);
85
86
        file_put_contents($file, $source);
87
88
        return $data;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function delete($id)
95
    {
96
        $file = $this->getFilePath($id);
97
98
        unlink($file);
99
    }
100
101
    /**
102
     * Calculate the id of a new row.
103
     * 
104
     * @param array $data
105
     * 
106
     * @return string
107
     */
108
    protected function getId(array $data)
109
    {
110
        return uniqid();
111
    }
112
113
    /**
114
     * Returns the path of a file.
115
     * 
116
     * @return string
117
     */
118
    protected function getFilePath($filename)
119
    {
120
        return $this->getBasePath()."/{$filename}.{$this->extension}";
121
    }
122
123
    /**
124
     * Transform the data to a string.
125
     * 
126
     * @param array $data
127
     * 
128
     * @return string
129
     */
130
    abstract protected function stringify(array $data);
131
132
    /**
133
     * Transform the string to an array.
134
     * 
135
     * @param string $source
136
     * 
137
     * @return array
138
     */
139
    abstract protected function parse($source);
140
}
141