Passed
Push — develop ( 070db2...bbac33 )
by BENARD
19:02 queued 12:47
created

AvatarUpload   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 30
c 0
b 0
f 0
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 33 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Team\Avatar;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Exception\ORMException;
9
use League\Flysystem\FilesystemException;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
use VideoGamesRecords\CoreBundle\Manager\AvatarManager;
16
use VideoGamesRecords\CoreBundle\Security\UserProvider;
17
18
class AvatarUpload extends AbstractController
19
{
20
    private AvatarManager $avatarManager;
21
    private UserProvider $userProvider;
22
    private EntityManagerInterface $em;
23
    private TranslatorInterface $translator;
24
25
26
    private array $extensions = array(
27
        'image/png' => '.png',
28
        'image/jpeg' => '.jpg',
29
    );
30
31
    public function __construct(
32
        AvatarManager $avatarManager,
33
        UserProvider $userProvider,
34
        EntityManagerInterface $em,
35
        TranslatorInterface $translator
36
    ) {
37
        $this->avatarManager = $avatarManager;
38
        $this->userProvider = $userProvider;
39
        $this->em = $em;
40
        $this->translator = $translator;
41
    }
42
43
    /**
44
     * @param Request $request
45
     * @return Response
46
     * @throws FilesystemException|ORMException
47
     */
48
    public function __invoke(Request $request): Response
49
    {
50
        $team = $this->userProvider->getTeam();
51
52
        $data = json_decode($request->getContent(), true);
53
        $file = $data['file'];
54
        $fp1 = fopen($file, 'r');
55
        $meta = stream_get_meta_data($fp1);
56
        $mimeType = $meta['mediatype'];
57
58
        $data = explode(',', $file);
59
60
        if (!array_key_exists($meta['mediatype'], $this->extensions)) {
61
            return new JsonResponse(
62
                [
63
                'message' => $this->translator->trans('avatar.extension_not_allowed'),
64
                ],
65
                400
66
            );
67
        }
68
69
        // Set filename
70
        $filename = $team->getId() . '_' . uniqid() . '.' . $this->avatarManager->getExtension($mimeType);
71
72
        $this->avatarManager->write($filename, base64_decode($data[1]));
73
74
        // Save avatar
75
        $team->setLogo($filename);
76
        $this->em->flush();
77
78
        return new JsonResponse([
79
            'message' => $this->translator->trans('avatar.success'),
80
        ], 200);
81
    }
82
}
83