1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* inetprocess/transformation |
4
|
|
|
* |
5
|
|
|
* php version 5.3 |
6
|
|
|
* |
7
|
|
|
* @author rémi sauvat |
8
|
|
|
* @copyright 2005-2015 inet process |
9
|
|
|
* |
10
|
|
|
* @package inetprocess/transformation |
11
|
|
|
* |
12
|
|
|
* @license gnu general public license v2.0 |
13
|
|
|
* |
14
|
|
|
* @link http://www.inetprocess.com |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Inet\Transformation\Rule; |
18
|
|
|
|
19
|
|
|
use Inet\Transformation\Exception\TransformationException; |
20
|
|
|
use Inet\Transformation\Exception\NotTransformableException; |
21
|
|
|
use Rhumsaa\Uuid\Uuid; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Call a function and send back the result |
25
|
|
|
*/ |
26
|
|
|
class CopyFileToUuid extends AbstractRule |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Operate the transformation |
30
|
|
|
* If input is an array each cell is replaced independently |
31
|
|
|
* and an array is returned |
32
|
|
|
* |
33
|
|
|
* @param mixed $input |
34
|
|
|
* @param array $arguments |
35
|
|
|
* |
36
|
|
|
* @throws Inet\Transformation\Exception\TransformationException |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
9 |
|
public function transform($input, array $arguments) |
41
|
|
|
{ |
42
|
|
|
// Bypass for empty $input |
|
|
|
|
43
|
9 |
|
if ($input === null || $input === '') { |
44
|
1 |
|
return null; |
45
|
|
|
} |
46
|
|
|
// Check input |
47
|
8 |
|
if (count($arguments) !== 2) { |
48
|
3 |
|
throw new TransformationException( |
49
|
|
|
'Rule CopyFileToUuid expects exactly 2 argument' |
50
|
3 |
|
); |
51
|
|
|
} |
52
|
5 |
|
$source_root = $arguments[0]; |
53
|
5 |
|
if (!is_string($source_root) || !is_dir($source_root)) { |
54
|
1 |
|
throw new TransformationException( |
55
|
|
|
'First argument of CopyFileToUuid should be a valid directory' |
56
|
1 |
|
); |
57
|
|
|
} |
58
|
4 |
|
$destination_path = $arguments[1]; |
59
|
4 |
|
if (!is_string($destination_path) || !is_dir($destination_path)) { |
60
|
1 |
|
throw new TransformationException( |
61
|
|
|
'Second argument of CopyFileToUuid should be a valid directory' |
62
|
1 |
|
); |
63
|
|
|
} |
64
|
3 |
|
$file_path = $source_root.'/'.$input; |
65
|
3 |
|
if (!is_file($file_path)) { |
66
|
1 |
|
throw new NotTransformableException( |
67
|
1 |
|
'Input file "'.$file_path.'" not found' |
68
|
1 |
|
); |
69
|
|
|
} |
70
|
|
|
// Transform |
71
|
2 |
|
$uuid = Uuid::uuid4()->toString(); |
72
|
2 |
|
$uuid_path = $destination_path.'/'.$uuid; |
73
|
2 |
|
if (!@copy($file_path, $uuid_path)) { |
74
|
1 |
|
$error = error_get_last(); |
75
|
1 |
|
throw new NotTransformableException( |
76
|
1 |
|
'Error in copy of file "'.$file_path.'" to "'.$uuid_path.'": '.$error['message'] |
77
|
1 |
|
); |
78
|
|
|
} |
79
|
1 |
|
return $uuid; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.