Preview   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 65
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreviewIcon() 0 3 1
A isPreviewAvailable() 0 3 1
A getIcon() 0 10 3
A create() 0 12 2
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload\formatter\preview;
5
6
use tkanstantsin\fileupload\model\IFile;
7
use tkanstantsin\fileupload\model\Type;
8
use yii\helpers\Url;
0 ignored issues
show
Bug introduced by
The type yii\helpers\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Class PreviewCreator
12
 * @todo: refactor.
13
 */
14
class Preview
15
{
16
    public const DEFAULT_PREVIEW = 'default';
17
18
    /**
19
     * @var array
20
     */
21
    public static $iconPreviewArray = [
22
        'file' => ['txt', 'doc',],
23
    ];
24
25
    /**
26
     * @var array
27
     */
28
    public static $availableExtensionArray = [
29
        'jpg', 'jpeg', 'png',
30
    ];
31
32
    /**
33
     * @param IFile $file
34
     * @return mixed|string
35
     * @throws \yii\base\InvalidParamException
36
     */
37
    public static function create(IFile $file)
38
    {
39
        return static::isPreviewAvailable($file)
40
            // TODO: move into component.
41
            ? Url::to([
42
                '/file/get',
43
                'id' => $file->getId(),
44
                'updatedAt' => $file->getUpdatedAt(),
45
                'fileType' => Type::IMAGE,
46
                'fileName' => $file->getFullName(),
47
            ])
48
            : static::getIcon($file);
49
    }
50
51
    /**
52
     * @param IFile $file
53
     * @return mixed
54
     */
55
    public static function getIcon(IFile $file)
56
    {
57
        // return icon
58
        foreach (static::$iconPreviewArray as $preview => $extensionArray) {
59
            if (\in_array($file->getExtension(), $extensionArray, true)) {
60
                return static::getPreviewIcon($preview);
61
            }
62
        }
63
64
        return static::getPreviewIcon(self::DEFAULT_PREVIEW);
65
    }
66
67
    public static function getPreviewIcon($preview)
68
    {
69
        return $preview;
70
    }
71
72
    /**
73
     * @param IFile $file
74
     * @return bool
75
     */
76
    protected static function isPreviewAvailable(IFile $file): bool
77
    {
78
        return \in_array($file->getExtension(), static::$availableExtensionArray, true);
79
    }
80
}
81