Passed
Push — master ( b91050...6fb96f )
by Jens
02:59
created

FilesStorage::addFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace CloudControl\Cms\storage\storage;
7
8
9
use CloudControl\Cms\storage\factories\FileFactory;
10
11
class FilesStorage extends AbstractStorage
12
{
13
    protected $filesDir;
14
15
    /**
16
     * @param \CloudControl\Cms\storage\Repository $repository
17
     * @param string $filesDir
18
     */
19
    public function __construct($repository, $filesDir)
20
    {
21
        parent::__construct($repository);
22
        $this->filesDir = $filesDir;
23
    }
24
25
26
    /**
27
     * @return array
28
     */
29
    public function getFiles()
30
    {
31
        $files = $this->repository->files;
32
        usort($files, array($this, 'compareFiles'));
33
34
        return $files;
35
    }
36
37
    /**
38
     * @param $postValues
39
     *
40
     * @return \CloudControl\Cms\storage\entities\File
41
     * @throws \Exception
42
     */
43
    public function addFile($postValues)
44
    {
45
        $destinationPath = $this->getDestinationPath();
46
47
        $filename = $this->validateFilename($postValues['name'], $destinationPath);
48
        $destination = $destinationPath . '/' . $filename;
49
50
        if ($postValues['error'] != '0') {
51
            throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
52
        }
53
54
        if (move_uploaded_file($postValues['tmp_name'], $destination)) {
55
            $file = FileFactory::createFileFromPostValues($postValues, $filename);
56
57
            $files = $this->repository->files;
58
            $files[] = $file;
59
            $this->repository->files = $files;
60
            $this->save();
61
        } else {
62
            throw new \Exception('Error moving uploaded file');
63
        }
64
        return $file;
65
    }
66
67
    /**
68
     * @param $filename
69
     *
70
     * @return \stdClass|null
71
     */
72
    public function getFileByName($filename)
73
    {
74
        $files = $this->getFiles();
75
        foreach ($files as $file) {
76
            if ($filename == $file->file) {
77
                return $file;
78
            }
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * @param $filename
86
     *
87
     * @throws \Exception
88
     */
89 View Code Duplication
    public function deleteFileByName($filename)
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...
90
    {
91
        $destinationPath = $this->getDestinationPath();
92
        $destination = $destinationPath . '/' . $filename;
93
94
        if (file_exists($destination)) {
95
            $files = $this->getFiles();
96
            foreach ($files as $key => $file) {
97
                if ($file->file == $filename) {
98
                    unlink($destination);
99
                    unset($files[$key]);
100
                }
101
            }
102
103
            $files = array_values($files);
104
            $this->repository->files = $files;
105
            $this->save();
106
        }
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getFilesDir()
113
    {
114
        return $this->filesDir;
115
    }
116
117
    /**
118
     * @param $a
119
     * @param $b
120
     *
121
     * @return int
122
     */
123
    private function compareFiles($a, $b)
124
    {
125
        return strcmp($a->file, $b->file);
126
    }
127
128
    protected function getDestinationPath()
129
    {
130
        $destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR);
131
        return $destinationPath;
132
    }
133
}