AttachFile   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 12
c 2
b 0
f 2
lcom 1
cbo 0
dl 0
loc 105
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteFile() 0 14 2
A fullFilePathDir() 0 4 1
A fullFilePath() 0 4 1
A getFilePath() 0 4 1
A getFilePathDir() 0 4 1
A getProjectPath() 0 3 1
A issetFile() 0 4 2
A isImage() 0 12 3
1
<?php
2
3
namespace ItBlaster\AttachFileBundle\Model;
4
5
use ItBlaster\AttachFileBundle\Model\om\BaseAttachFile;
6
7
class AttachFile extends BaseAttachFile
8
{
9
    /**
10
     * Удаление файла
11
     *
12
     * @return bool
13
     */
14
    public function deleteFile()
15
    {
16
        $file_path = $this->fullFilePath();
17
        if (is_file($file_path)) {
18
            unlink($file_path);
19
        }
20
        $this
21
            ->setOriginalName("")
22
            ->setFileName("")
23
            ->setExt("")
24
            ->setSize("")
25
            ->save();
26
        return true;
27
    }
28
29
    /**
30
     * Полный путь до папки файла на сервере
31
     *
32
     * @return string
33
     */
34
    public function fullFilePathDir()
35
    {
36
        return $this->getProjectPath().$this->getFilePathDir();
37
    }
38
39
    /**
40
     * Полный путь файла на сервере
41
     *
42
     * @return string
43
     */
44
    public function fullFilePath()
45
    {
46
        return $this->getProjectPath().$this->getFilePath();
47
    }
48
49
    /**
50
     * Путь до файла
51
     *
52
     * /uploads/productmateriali18n/54956796bdc5c.pdf
53
     *
54
     * @return string
55
     */
56
    public function getFilePath()
57
    {
58
        return $this->getFilePathDir().$this->getFileName().".".$this->getExt();
59
    }
60
61
    /**
62
     * Папка, где лежит файл
63
     *
64
     * @return string
65
     */
66
    protected function getFilePathDir()
67
    {
68
        return "/uploads/".$this->getModel()."/".$this->getObjectId()."/";
69
    }
70
71
    /**
72
     * Папка проекта на сервере
73
     *
74
     * TODO: написать специальный бихейвор для класса AttachFile,
75
     * который будет в BaseAttachFile прописывать путь project_path
76
     * на основе AppKernel
77
     *
78
     * @return mixed
79
     */
80
    public function getProjectPath(){
0 ignored issues
show
Coding Style introduced by
getProjectPath uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
81
        return $_SERVER["DOCUMENT_ROOT"];
82
    }
83
84
    /**
85
     * Существует ли файл физически на сервере
86
     *
87
     * @return bool
88
     */
89
    public function issetFile()
90
    {
91
        return $this->getFileName() && file_exists($this->fullFilePath());
92
    }
93
94
    /**
95
     * Является ли файл изображением
96
     *
97
     * @return bool
98
     */
99
    public function isImage()
100
    {
101
        //$_SERVER["DOCUMENT_ROOT"] = '/home/user_name/projects/project_name/web';
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
102
        if ($this->issetFile()) {
103
            $mime_type = mime_content_type($this->fullFilePath());
104
            if ($mime_type) {
105
                $mime_type_params = explode('/', $mime_type);
106
                return $mime_type_params[0] == 'image';
107
            }
108
        }
109
        return false;
110
    }
111
}
112