SourceRepository   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelativeFolderPath() 0 9 1
A getFolderPath() 0 10 2
A getFilePath() 0 4 1
B getFileType() 0 21 6
A getFileSrc() 0 12 3
A getUploadDir() 0 4 1
A getUploadUrl() 0 9 3
1
<?php
2
3
namespace yiicod\fileupload\models\behaviors;
4
5
use Yii;
6
use yii\db\ActiveRecord;
7
use yii\helpers\FileHelper;
8
use yii\helpers\Url;
9
use yii\web\Application;
10
11
/**
12
 * Class SourceRepository
13
 *
14
 * @package yiicod\fileupload\models\behaviors
15
 */
16
class SourceRepository implements SourceRepositoryInterface
17
{
18
    /**
19
     * @var ActiveRecord
20
     */
21
    public $owner;
22
23
    /**
24
     * The public url for file
25
     * 'url' => 'uploads'
26
     */
27
    public $uploadUrl;
28
29
    /**
30
     * The directory path for file
31
     * 'uploadDir' => Yii::getAlias('@webroot/uploads')
32
     */
33
    public $uploadDir;
34
35
    /**
36
     * Get relative folder path
37
     *
38
     * @return string
39
     */
40
    public function getRelativeFolderPath(): string
41
    {
42
        $primaryKey = ActiveRecord::getDb()
43
            ->getSchema()
44
            ->getTableSchema(get_class($this->owner)::collectionName())
0 ignored issues
show
Bug introduced by
The method collectionName cannot be called on get_class($this->owner) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
            ->primaryKey;
46
47
        return '/' . lcfirst(get_class($this->owner)::tableName()) . '/' . $this->owner->{$primaryKey};
0 ignored issues
show
Bug introduced by
The method tableName cannot be called on get_class($this->owner) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
48
    }
49
50
    /**
51
     * Get folder path
52
     *
53
     * @param string $field Field name
54
     *
55
     * @author Orlov Alexey <[email protected]>
56
     *
57
     * @return string Return path to entity img with|out field name
58
     */
59
    public function getFolderPath(string $field = ''): string
60
    {
61
        $path = rtrim($this->getUploadDir(), '/') . $this->getRelativeFolderPath();
62
63
        if ('' === $field) {
64
            return $path;
65
        }
66
67
        return rtrim($path, '/') . '/' . $field . '/';
68
    }
69
70
    /**
71
     * Get file path by "getFolderPath"
72
     *
73
     * @param string $field Field name
74
     *
75
     * @author Orlov Alexey <[email protected]>
76
     *
77
     * @return string Return path to file
78
     */
79
    public function getFilePath(string $field): string
80
    {
81
        return rtrim($this->getFolderPath($field), '/') . '/' . $this->owner->{$field};
82
    }
83
84
    /**
85
     * Get file type
86
     *
87
     * @author Orlov Alexey <[email protected]>
88
     *
89
     * @param string $field Field name
90
     * @param bool $full Full or not full file type
91
     *
92
     * @todo If no need then delete in the next version
93
     *
94
     * @return string Return file type
95
     */
96
    public function getFileType(string $field, $full = true)
97
    {
98
        $filePath = $this->getFilePath($field);
99
        if (empty($this->owner->{$field}) || !file_exists($filePath)) {
100
            return '';
101
        }
102
103
        $mimeType = FileHelper::getMimeType($filePath);
104
        if (null === $mimeType) {
105
            $ext = pathinfo($filePath, PATHINFO_EXTENSION);
106
            $mimeType = FileHelper::getMimeTypeByExtension($ext);
107
        }
108
109
        if (true === $full) {
110
            return $mimeType;
111
        }
112
113
        $mimeContentType = explode('/', $mimeType);
114
115
        return isset($mimeContentType[1]) ? $mimeContentType[1] : false;
116
    }
117
118
    /**
119
     * Get file src
120
     *
121
     * @author Orlov Alexey <[email protected]>
122
     *
123
     * @param $field
124
     * @param null $default
125
     *
126
     * @return string File src
127
     *
128
     * @throws Exception
129
     */
130
    public function getFileSrc(string $field, $default = null): string
131
    {
132
        if (empty($this->owner->{$field})) {
133
            $url = null === $default ? '' : $default;
134
        } else {
135
            $url = '';
136
            $url .= $this->getUploadUrl();
137
            $url .= rtrim($this->getRelativeFolderPath(), '/') . '/' . $field . '/' . $this->owner->{$field};
138
        }
139
140
        return $url;
141
    }
142
143
    /**
144
     * Base upload dir
145
     *
146
     * @return string
147
     */
148
    public function getUploadDir(): string
149
    {
150
        return $this->uploadDir;
151
    }
152
153
    /**
154
     * Base upload url
155
     *
156
     * @return string
157
     */
158
    public function getUploadUrl(): string
159
    {
160
        if (Yii::$app instanceof Application) {
161
            $this->uploadUrl = 0 === strpos($this->uploadUrl, Url::base(true)) ?
162
                $this->uploadUrl : trim(Url::base(true), '/') . '/' . trim($this->uploadUrl, '/');
163
        }
164
165
        return $this->uploadUrl;
166
    }
167
}
168