FilePreviewFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 46
ccs 0
cts 22
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createGenerator() 0 11 2
A getMimeType() 0 4 1
A resolveGeneratorClass() 0 10 3
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets\filePreview;
12
13
use hipanel\helpers\FileHelper;
14
use hipanel\widgets\filePreview\types\ImagePreviewGenerator;
15
use hipanel\widgets\filePreview\types\PdfPreviewGenerator;
16
use hipanel\widgets\filePreview\types\PreviewGeneratorInterface;
17
use Yii;
18
19
/**
20
 * Class FilePreviewFactory creates File.
21
 */
22
class FilePreviewFactory implements FilePreviewFactoryInterface
23
{
24
    public $generators = [
25
        '^image/.*$' => ImagePreviewGenerator::class,
26
//        '^application/pdf$' => PdfPreviewGenerator::class,
27
    ];
28
29
    /**
30
     * @param string $path
31
     * @throws UnsupportedMimeTypeException
32
     * @return PreviewGeneratorInterface
33
     */
34
    public function createGenerator($path)
35
    {
36
        $mime = $this->getMimeType($path);
37
38
        $className = $this->resolveGeneratorClass($mime);
39
        if ($className === false) {
40
            throw new UnsupportedMimeTypeException();
41
        }
42
43
        return Yii::createObject($className, [$path]);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getMimeType($path)
50
    {
51
        return FileHelper::getMimeType($path);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function resolveGeneratorClass($mimeType)
58
    {
59
        foreach ($this->generators as $pattern => $generatorClass) {
60
            if (preg_match('#' . $pattern . '#', $mimeType)) {
61
                return $generatorClass;
62
            }
63
        }
64
65
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface hipanel\widgets\filePrev...::resolveGeneratorClass of type League\FactoryMuffin\Generators\GeneratorInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
    }
67
}
68