AssetFactory   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 36
c 3
b 1
f 1
lcom 0
cbo 4
dl 0
loc 103
rs 8.8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 21 4
C guessType() 0 58 32
1
<?php
2
3
namespace Libcast\AssetDistributor\Asset;
4
5
use League\Flysystem\Adapter\Local;
6
use League\Flysystem\File;
7
use League\Flysystem\Filesystem;
8
9
class AssetFactory
10
{
11
    const AUDIO = 'Audio';
12
    const DOCUMENT = 'Document';
13
    const IMAGE = 'Image';
14
    const VIDEO = 'Video';
15
16
    /**
17
     *
18
     * @param $file
19
     * @param null $title
20
     * @param null $description
21
     * @param array $tags
22
     * @param null $category
23
     * @return Audio|Document|Image|Video
24
     * @throws \Exception
25
     */
26
    public static function build($file, $title = null, $description = null, array $tags = [], $category = null)
27
    {
28
        if (!$file instanceof File and is_file($file)) {
29
            $local = new Filesystem(new Local(dirname($file)));
30
            $file = new File($local, basename($file));
31
        }
32
33
        if (!$type = self::guessType($file->getMimetype())) {
34
            throw new \Exception('This file is not supported');
35
        }
36
37
        $class = sprintf('\Libcast\AssetDistributor\Asset\%s', $type);
38
39
        $asset = new $class($file); /** @var $asset AbstractAsset */
40
        $asset->setTitle($title);
41
        $asset->setDescription($description);
42
        $asset->setTags($tags);
43
        $asset->setCategory($category);
44
45
        return $asset;
46
    }
47
48
    /**
49
     *
50
     * @param $mimetype
51
     * @return null
52
     */
53
    private static function guessType($mimetype)
54
    {
55
        $mimetype = strtolower($mimetype);
56
57
        switch ($mimetype) {
58
            case 'application/ogg':
59
                return self::AUDIO;
60
61
            case 'application/excel':
62
            case 'application/x-msexcel':
63
            case 'application/vnd.ms-excel':
64
            case 'application/vnd.ms-excel.sheet.macroenabled.12':
65
            case 'application/vnd.ms-office':
66
            case 'application/vnd.oasis.opendocument.text':
67
            case 'application/vnd.sealed.xls':
68
            case 'application/vnd.sealedmedia.softseal.pdf':
69
            case 'application/msword':
70
            case 'application/pdf':
71
            case 'application/x-pdf':
72
            case 'application/mspowerpoint':
73
            case 'application/vnd.ms-powerpoint':
74
            case 'application/postscript':
75
            case 'application/rtf':
76
            case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
77
            case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
78
            case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
79
                return self::DOCUMENT;
80
81
            case 'application/eps':
82
            case 'application/pcx':
83
            case 'application/x-pcx':
84
            case 'video/x-mng ':
85
                return self::IMAGE;
86
87
            case 'application/mp4':
88
            case 'application/mxf':
89
            case 'application/ogv':
90
            case 'application/vnd.ms-asf':
91
            case 'application/vnd.rn-realmedia-vbr':
92
            case 'application/vnd.rn-realmedia':
93
                return self::VIDEO;
94
        }
95
96
        $types = [
97
            self::AUDIO,
98
            self::DOCUMENT,
99
            self::IMAGE,
100
            self::VIDEO,
101
        ];
102
103
        foreach ($types as $type) {
104
            if (strtolower($type) === strstr($mimetype, '/', true)) {
105
                return $type;
106
            }
107
        }
108
109
        return null;
110
    }
111
}
112