Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

File   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 110
Duplicated Lines 3.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 4
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A guessExtension() 0 4 1
A guessMimeType() 0 4 1
A move() 4 13 2
A getTargetFile() 0 14 6
A customChmod() 0 6 2
A getName() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\File;
6
7
use Pivasic\Bundle\Common\File\Exception\FileException;
8
use Pivasic\Bundle\Common\File\Exception\FileNotFoundException;
9
use Pivasic\Bundle\Common\File\Extension\Extension;
10
use Pivasic\Bundle\Common\File\MimeType\MimeType;
11
12
/**
13
 * Class File
14
 * @package Pivasic\Bundle\Common\File
15
 */
16
class File extends \SplFileInfo
17
{
18
    /**
19
     * @param string $path      The path to the file
20
     * @param bool   $checkPath Whether to check the path or not
21
     * @throws FileNotFoundException
22
     */
23
    public function __construct($path, $checkPath = true)
24
    {
25
        if ($checkPath && !is_file($path)) {
26
            throw new FileNotFoundException($path);
27
        }
28
29
        parent::__construct($path);
30
    }
31
32
    /**
33
     * @return string|null
34
     */
35
    public function guessExtension()
36
    {
37
        return Extension::getInstance()->guess($this->guessMimeType());
38
    }
39
40
    /**
41
     * @return string|null
42
     */
43
    public function guessMimeType()
44
    {
45
        return MimeType::getInstance()->guess($this->getPathname());
46
    }
47
48
    /**
49
     * Moves the file to a new location.
50
     *
51
     * @param string $directory   Destination folder
52
     * @param string|null $name   New file name
53
     *
54
     * @return File
55
     *
56
     * @throws FileException
57
     */
58
    public function move($directory, $name = null)
59
    {
60
        $target = $this->getTargetFile($directory, $name);
61
62 View Code Duplication
        if (!@rename($this->getPathname(), $target)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            $error = error_get_last();
64
            throw new FileException(sprintf('Could not rename the file "%s" (%s)', $this->getPathname(), strip_tags($error['message'])));
65
        }
66
67
       $this->customChmod($target);
68
69
        return $target;
70
    }
71
72
    /**
73
     * @param $directory
74
     * @param string|null $name
75
     *
76
     * @return File
77
     *
78
     * @throws FileException
79
     */
80
    protected function getTargetFile($directory, $name = null)
81
    {
82
        if (!is_dir($directory)) {
83
            if (false === @mkdir($directory, 0777, true) && !is_dir($directory)) {
84
                throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
85
            }
86
        } elseif (!is_writable($directory)) {
87
            throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
88
        }
89
90
        $target = rtrim($directory, '/\\') . DIRECTORY_SEPARATOR . (null === $name ? $this->getBasename() : $this->getName($name));
91
92
        return new self($target, false);
93
    }
94
95
    /**
96
     * Chmod function with exception
97
     *
98
     * @param $target
99
     * @param $mode
100
     *
101
     * @throws FileException
102
     */
103
    protected function customChmod($target, $mode = 0666)
104
    {
105
        if (false === @chmod($target, $mode & ~umask())) {
106
            throw new FileException(sprintf('Unable to change mode of the "%s"', $target));
107
        }
108
    }
109
110
    /**
111
     * Returns locale independent base name of the given path.
112
     *
113
     * @param string $name The new file name
114
     *
115
     * @return string containing
116
     */
117
    protected function getName($name)
118
    {
119
        $originalName = str_replace('\\', '/', $name);
120
        $pos = strrpos($originalName, '/');
121
        $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
122
123
        return $originalName;
124
    }
125
}