FileManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 85
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilesRoot() 0 4 1
A setFilesBaseUrl() 0 4 1
A init() 0 7 2
A generateFileName() 0 11 3
A getImageSrc() 0 4 1
A getImageUrl() 0 4 1
A deleteFile() 0 5 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-banner
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\banner\common\components;
9
10
use Yii;
11
use yii\base\Object;
12
use yii\helpers\FileHelper;
13
14
/**
15
 * File manager for work with image files.
16
 *
17
 * @author Vladimir Kuprienko <[email protected]>
18
 * @since 1.0
19
 */
20
class FileManager extends Object implements FileManagerInterface
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\Object has been deprecated with message: since 2.0.13, the class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    /**
23
     * Path to catalog where you store uploaded files.
24
     *
25
     * @var string
26
     */
27
    protected $filesRoot = '@webroot/uploads/banners';
28
    /**
29
     * URL of uploaded files catalog.
30
     *
31
     * @var string
32
     */
33
    protected $filesBaseUrl = '@web/uploads/banners';
34
35
36
    /**
37
     * @param $filesRoot
38
     */
39
    public function setFilesRoot($filesRoot)
40
    {
41
        $this->filesRoot =$filesRoot;
42
    }
43
44
    /**
45
     * @param $filesBaseUrl
46
     */
47
    public function setFilesBaseUrl($filesBaseUrl)
48
    {
49
        $this->filesBaseUrl = $filesBaseUrl;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function init()
56
    {
57
        $filesRoot = Yii::getAlias($this->filesRoot);
58
        if (!file_exists($filesRoot)) {
59
            FileHelper::createDirectory($filesRoot);
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function generateFileName($extension)
67
    {
68
        $fileName = '';
69
        while (true) {
70
            $fileName = uniqid('', true) . '.' . $extension;
71
            if (!file_exists($this->getImageSrc($fileName))) {
72
                break;
73
            }
74
        }
75
        return $fileName;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     * @return bool|string
81
     */
82
    public function getImageSrc($fileName)
83
    {
84
        return Yii::getAlias($this->filesRoot . '/' . $fileName, false);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     * @return bool|string
90
     */
91
    public function getImageUrl($fileName)
92
    {
93
        return Yii::getAlias($this->filesBaseUrl . '/' . $fileName, false);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function deleteFile($fileName)
100
    {
101
        $file = $this->getImageSrc($fileName);
102
        return file_exists($file) ? unlink($file) : false;
103
    }
104
}
105