AssetFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 35.53 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 27
loc 76
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromPath() 0 8 2
A createFromSplFileInfo() 13 13 1
A createFromUploadedFile() 14 14 1
A createFromUri() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the limit0/assets package.
5
 *
6
 * (c) Limit Zero, LLC <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Limit0\Assets;
13
14
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
17
/**
18
 * The AssetFactory is responsible for creating Asset instances from files or paths
19
 *
20
 * @author  Josh Worden <[email protected]>
21
 */
22
class AssetFactory
23
{
24
    /**
25
     * Creates an Asset from a file path
26
     *
27
     * @param   string          $path
28
     * @throws  Exception\FactoryException  If the file could not be loaded
29
     * @return  Asset
30
     */
31 10
    public static function createFromPath($path)
32
    {
33 10
        if (!file_exists($path)) {
34 1
            throw new Exception\FactoryException(sprintf('Unable to open file %s', $path));
35
        }
36 9
        $file = new \SplFileInfo($path);
37 9
        return static::createFromSplFileInfo($file);
38
    }
39
40
    /**
41
     * Creates an Asset from a \SplFileInfo wrapper
42
     *
43
     * @param   \SplFileInfo    $file
44
     * @throws  Exception\FactoryException  If the file could not be loaded
45
     * @return  Asset
46
     */
47 10 View Code Duplication
    public static function createFromSplFileInfo(\SplFileInfo $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 10
        $asset = new Asset();
50 10
        $guesser = MimeTypeGuesser::getInstance();
51
        return $asset
52 10
            ->setFilename($file->getFilename())
53 10
            ->setExtension($file->getExtension())
54 10
            ->setPathname($file->getPathname())
55 10
            ->setMimeType($guesser->guess($file->getPathname()))
56 10
            ->setClientOriginalName($file->getFilename())
57 10
            ->setClientOriginalExtension($file->getExtension())
58 10
        ;
59
    }
60
61
    /**
62
     * Creates an Asset from an UploadedFile instance
63
     *
64
     * @param   UploadedFile    $file
65
     * @throws  Exception\FactoryException  If the file could not be loaded
66
     * @return  Asset
67
     */
68 1 View Code Duplication
    public static function createFromUploadedFile(UploadedFile $file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 1
        $asset = new Asset();
71 1
        $guesser = MimeTypeGuesser::getInstance();
72
        return $asset
73 1
            ->setFilename($file->getFilename())
74 1
            ->setExtension($file->getExtension())
75 1
            ->setPathname($file->getPathname())
76 1
            ->setMimeType($guesser->guess($file->getPathname()))
77 1
            ->setClientOriginalName($file->getClientOriginalName())
78 1
            ->setClientOriginalExtension($file->getClientOriginalExtension())
79 1
            ->setClientMimeType($file->getClientMimeType())
80 1
        ;
81
    }
82
83
    /**
84
     * Creates an Asset from a URI
85
     *
86
     * @param   string          $uri
87
     * @throws  Exception\FactoryException  If the file could not be loaded
88
     * @return  Asset
89
     */
90
    public static function createFromUri($uri)
91
    {
92
        $name = substr($uri, strrpos($uri, '/'));
93
        $path = sprintf('%s/%s', sys_get_temp_dir(), $name);
94
        copy($uri, $path);
95
        return static::createFromPath($path);
96
    }
97
}
98