1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the limit0/assets package. |
5
|
|
|
* |
6
|
|
|
* (c) Limit Zero, LLC <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Limit0\Assets\StorageEngine; |
13
|
|
|
|
14
|
|
|
use Limit0\Assets\Asset; |
15
|
|
|
use Limit0\Assets\AssetFactory; |
16
|
|
|
use Limit0\Assets\Exception\StorageException; |
17
|
|
|
use Limit0\Assets\StorageEngineInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class supports storing, retrieve, and deleting files to a local server path |
21
|
|
|
* |
22
|
|
|
* @author Josh Worden <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class LocalStorageEngine implements StorageEngineInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
4 |
|
public function getPath() |
35
|
|
|
{ |
36
|
4 |
|
return $this->path; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
|
public function remove($identifier) |
43
|
|
|
{ |
44
|
1 |
|
$rootPath = sprintf('%s/%s', $this->getPath(), $identifier); |
45
|
|
|
|
46
|
1 |
|
if (!file_exists($rootPath)) { |
47
|
|
|
throw new StorageException(sprintf('File %s could not be found in %s', $identifier, $this->getPath())); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return unlink($rootPath); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
2 |
|
public function retrieve($identifier) |
57
|
|
|
{ |
58
|
2 |
|
$rootPath = sprintf('%s/%s', $this->getPath(), $identifier); |
59
|
|
|
|
60
|
2 |
|
if (!file_exists($rootPath)) { |
61
|
|
|
throw new StorageException(sprintf('File %s could not be found in %s', $identifier, $this->getPath())); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$path = explode('/', $identifier); |
65
|
2 |
|
$fileName = array_pop($path); |
66
|
2 |
|
$localPath = sprintf('%s/%s', sys_get_temp_dir(), $fileName); |
|
|
|
|
67
|
|
|
|
68
|
2 |
|
$asset = AssetFactory::createFromPath($rootPath); |
69
|
2 |
|
$asset->setFilename($fileName)->setFilepath(implode('/', $path)); |
70
|
2 |
|
return $asset; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the path assets will be stored under |
75
|
|
|
* |
76
|
|
|
* @param string |
77
|
|
|
*/ |
78
|
|
|
public function setPath($path) |
79
|
|
|
{ |
80
|
|
|
$this->path = $path; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function store(Asset $asset, $path = null, $filename = null) |
88
|
|
|
{ |
89
|
1 |
|
$filename = (null === $filename) ? $asset->getBasename() : $filename; |
90
|
1 |
|
$dir = sprintf('%s/%s', $this->getPath(), $path); |
91
|
1 |
|
$key = sprintf('%s/%s', $dir, $filename); |
92
|
|
|
|
93
|
1 |
|
if (!is_dir($dir) && true !== @mkdir($dir, 0777, true)) { |
94
|
|
|
$error = error_get_last(); |
95
|
|
|
throw new StorageException($error['message']); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
file_put_contents($key, file_get_contents($asset)); |
99
|
|
|
|
100
|
1 |
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.