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

AvatarController::upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 9.7
cc 2
nc 2
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller\Team;
4
5
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...
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
use Symfony\Contracts\Translation\TranslatorInterface;
12
use VideoGamesRecords\CoreBundle\Entity\Team;
13
use VideoGamesRecords\CoreBundle\Repository\TeamRepository;
14
use VideoGamesRecords\CoreBundle\Service\Team\AvatarManager;
15
16
/**
17
 * Class TeamController
18
 */
19
class AvatarController extends AbstractController
20
{
21
    private TranslatorInterface $translator;
22
    private TeamRepository $teamRepository;
23
    private AvatarManager $avatarManager;
24
25
    private array $extensions = array(
26
        'image/png' => '.png',
27
        'image/jpeg' => '.jpg',
28
    );
29
30
    public function __construct(TranslatorInterface $translator, TeamRepository $teamRepository, AvatarManager $avatarManager)
31
    {
32
        $this->translator = $translator;
33
        $this->teamRepository = $teamRepository;
34
        $this->avatarManager = $avatarManager;
35
    }
36
37
    /**
38
     * @param Team    $team
39
     * @param Request $request
40
     * @return Response
41
     * @throws FilesystemException
42
     */
43
    public function upload(Team $team, Request $request): Response
44
    {
45
        $data = json_decode($request->getContent(), true);
46
        $file = $data['file'];
47
        $fp1 = fopen($file, 'r');
48
        $meta = stream_get_meta_data($fp1);
49
        $mimeType = $meta['mediatype'];
50
51
        $data = explode(',', $file);
52
53
        if (!array_key_exists($meta['mediatype'], $this->extensions)) {
54
            return new JsonResponse([
55
                'message' => $this->translator->trans('avatar.extension_not_allowed'),
56
            ],
57
            400
58
            );
59
        }
60
61
        // Set filename
62
        $filename = $team->getId() . '_' . uniqid() . '.' . $this->avatarManager->getExtension($mimeType);
63
64
        $this->avatarManager->write($filename, base64_decode($data[1]));
65
66
        // Save avatar
67
        $team->setLogo($filename);
68
        $this->teamRepository->flush();
69
70
        return new JsonResponse([
71
            'message' => $this->translator->trans('avatar.success'),
72
        ], 200);
73
    }
74
75
76
    /**
77
     * @param Team $team
78
     * @return StreamedResponse
79
     * @throws FilesystemException
80
     */
81
    public function download(Team $team): StreamedResponse
82
    {
83
        return $this->avatarManager->read($team->getLogo());
84
    }
85
}
86