File::getAcceptedMimeTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 10
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Api\FileException;
8
use Application\Repository\FileRepository;
9
use Doctrine\ORM\Mapping as ORM;
10
use Ecodev\Felix\Model\Traits\HasName;
11
use GraphQL\Doctrine\Attribute as API;
12
use Psr\Http\Message\UploadedFileInterface;
13
use Throwable;
14
15
/**
16
 * An uploaded file that is related to a card (pdf/docx/xlsx/etc.).
17
 */
18
#[ORM\UniqueConstraint(name: 'unique_name', columns: ['filename'])]
19
#[ORM\HasLifecycleCallbacks]
20
#[ORM\Entity(FileRepository::class)]
21
class File extends AbstractModel implements \Ecodev\Felix\Model\File
22
{
23
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
24
    #[ORM\ManyToOne(targetEntity: Card::class)]
25
    private ?Card $card = null;
26
27
    use \Ecodev\Felix\Model\Traits\File {
28
        setFile as traitSetFile;
29
    }
30
    use HasName;
31
32
    public function setCard(Card $card): void
33
    {
34
        $this->card = $card;
35
    }
36
37
    public function getCard(): Card
38
    {
39
        return $this->card;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->card could return the type null which is incompatible with the type-hinted return Application\Model\Card. Consider adding an additional type-check to rule them out.
Loading history...
40
    }
41
42
    /**
43
     * Set the file.
44
     */
45
    #[API\Input(type: '?GraphQL\Upload\UploadType')]
46
    public function setFile(UploadedFileInterface $file): void
47
    {
48
        try {
49
            $this->traitSetFile($file);
50
51
            // Default to filename as name
52
            if (!$this->getName()) {
53
                $this->setName($file->getClientFilename());
0 ignored issues
show
Bug introduced by
It seems like $file->getClientFilename() can also be of type null; however, parameter $name of Application\Model\File::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                $this->setName(/** @scrutinizer ignore-type */ $file->getClientFilename());
Loading history...
54
            }
55
        } catch (Throwable $e) {
56
            throw new FileException($file, $e);
57
        }
58
    }
59
60
    protected function getAcceptedMimeTypes(): array
61
    {
62
        // This list should be kept in sync with FilesComponent.accept
63
        return [
64
            'application/msword',
65
            'application/pdf',
66
            'application/x-pdf',
67
            'application/vnd.ms-excel',
68
            'application/vnd.oasis.opendocument.spreadsheet',
69
            'application/vnd.oasis.opendocument.text',
70
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
71
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
72
        ];
73
    }
74
}
75