Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

FileAttributesResolver::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\File\Attributes\Resolver;
13
14
use Liip\ImagineBundle\File\Attributes\Attributes;
15
use Liip\ImagineBundle\File\Attributes\ContentTypeAttribute;
16
use Liip\ImagineBundle\File\Attributes\ExtensionAttribute;
17
use Liip\ImagineBundle\File\Attributes\Guesser\ContentTypeGuesserInterface;
18
use Liip\ImagineBundle\File\Attributes\Guesser\ExtensionGuesserInterface;
19
use Liip\ImagineBundle\File\FileBlobInterface;
20
use Liip\ImagineBundle\File\FileInterface;
21
use Liip\ImagineBundle\File\FilePathInterface;
22
use Liip\ImagineBundle\File\FileTemp;
23
24
/**
25
 * @author Rob Frawley 2nd <[email protected]>
26
 */
27
final class FileAttributesResolver
28
{
29
    /**
30
     * @var ContentTypeGuesserInterface
31
     */
32
    private $contentTypeGuesser;
33
34
    /**
35
     * @var ExtensionGuesserInterface
36
     */
37
    private $extensionGuesser;
38
39
    /**
40
     * @param ContentTypeGuesserInterface $contentTypeGuesser
41
     * @param ExtensionGuesserInterface   $extensionGuesser
42
     */
43
    public function __construct(ContentTypeGuesserInterface $contentTypeGuesser, ExtensionGuesserInterface $extensionGuesser)
44
    {
45
        $this->contentTypeGuesser = $contentTypeGuesser;
46
        $this->extensionGuesser = $extensionGuesser;
47
    }
48
49
    /**
50
     * @param FileInterface|string $file
51
     *
52
     * @return Attributes
53
     */
54
    public function resolve($file): Attributes
55
    {
56
        if ($file instanceof FileBlobInterface) {
57
            return $this->resolveFileBlob($file);
58
        }
59
60
        if ($file instanceof FilePathInterface) {
61
            return $this->resolveFilePath($file);
62
        }
63
64
        return $this->guess($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by parameter $file on line 54 can also be of type object<Liip\ImagineBundle\File\FileInterface>; however, Liip\ImagineBundle\File\...ibutesResolver::guess() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
65
    }
66
67
    /**
68
     * @param FileInterface $file
69
     *
70
     * @return Attributes
71
     */
72
    public function resolveFileBlob(FileInterface $file): Attributes
73
    {
74
        $temporary = new FileTemp('file-attributes-resolver');
75
        $temporary->acquire();
76
77
        if ($file->hasContents()) {
78
            $temporary->setContents($file->getContents());
79
        }
80
81
        try {
82
            return $this->resolveFilePath($temporary);
83
        } finally {
84
            $temporary->release();
85
        }
86
    }
87
88
    /**
89
     * @param FilePathInterface $file
90
     *
91
     * @return Attributes
92
     */
93
    public function resolveFilePath(FilePathInterface $file): Attributes
94
    {
95
        return $this->guess($file->getFile()->getPathname());
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return Attributes
102
     */
103
    private function guess(string $path): Attributes
104
    {
105
        $c = ContentTypeAttribute::create(
106
            $this->contentTypeGuesser->guess($path)
107
        );
108
109
        $e = ExtensionAttribute::create(
110
            $this->extensionGuesser->guess($c->stringify())
111
        );
112
113
        return new Attributes($c, $e);
114
    }
115
}
116