Passed
Branch develop (a13b53)
by BENARD
12:48
created

AvatarManager::getExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace VideoGamesRecords\CoreBundle\Service\Team;
3
4
use League\Flysystem\FilesystemException;
0 ignored issues
show
Bug introduced by
The type League\Flysystem\FilesystemException 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...
5
use League\Flysystem\FilesystemOperator;
0 ignored issues
show
Bug introduced by
The type League\Flysystem\FilesystemOperator 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...
6
use Symfony\Component\HttpFoundation\StreamedResponse;
7
8
class AvatarManager
9
{
10
    private string $prefix = 'team/';
11
12
    private array $extensions = array(
13
        'png' => 'image/png',
14
        'jpg' => 'image/jpeg'
15
    );
16
17
    private FilesystemOperator $vgrCoreStorage;
18
19
20
    public function __construct(FilesystemOperator $vgrCoreStorage)
21
    {
22
        $this->vgrCoreStorage = $vgrCoreStorage;
23
    }
24
25
    /**
26
     * @throws FilesystemException
27
     */
28
    public function write(string $filename, string $contents): void
29
    {
30
         $this->vgrCoreStorage->write($this->prefix . $filename, $contents);
31
    }
32
33
34
    /**
35
     * @throws FilesystemException
36
     */
37
    public function read(string $filename): StreamedResponse
38
    {
39
        $path = $this->prefix . $filename;
40
        if (!$this->vgrCoreStorage->fileExists($path)) {
41
            $path = $this->prefix . 'default.png';
42
        }
43
44
        $stream = $this->vgrCoreStorage->readStream($path);
45
        return new StreamedResponse(function () use ($stream) {
46
            fpassthru($stream);
47
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
        }, 200, ['Content-Type' => $this->getMimeType($path)]);
49
    }
50
51
52
    public function getAllowedMimeType(): array
53
    {
54
        return array_values($this->extensions);
55
    }
56
57
    public function getExtension($mimeType): string
58
    {
59
        $types = array_flip($this->extensions);
60
        return $types[$mimeType] ?? 'png';
61
    }
62
63
    private function getMimeType(string $file): string
64
    {
65
        $infos = pathinfo($file);
66
        return $this->extensions[$infos['extension']] ?? 'image/png';
67
    }
68
}
69