Completed
Push — master ( 323fd4...3d3329 )
by Rémi
02:27
created

CopyFileToUuid::transform()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 27
cts 27
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 25
nc 7
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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