Passed
Pull Request — master (#631)
by ANTHONIUS
08:16
created

UploadHandler::findApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 1
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Applications\Service;
6
7
8
use Applications\Entity\Application;
9
use Applications\Entity\ApplicationInterface;
10
use Applications\Entity\Attachment;
11
use Core\Entity\FileMetadata;
12
use Core\Service\FileManager;
13
use Core\Service\UploadedFileInfo;
14
use Doctrine\ODM\MongoDB\DocumentManager;
15
use Doctrine\Persistence\ObjectRepository;
16
17
class UploadHandler
18
{
19
    /**
20
     * @var DocumentManager
21
     */
22
    private DocumentManager $dm;
23
    /**
24
     * @var FileManager
25
     */
26
    private FileManager $fileManager;
27
28
    /**
29
     * @var ObjectRepository
30
     */
31
    private ObjectRepository $appRepository;
32
33
    public function __construct(
34
        DocumentManager $dm,
35
        FileManager $fileManager,
36
        ObjectRepository $appRepository
37
    )
38
    {
39
        $this->dm = $dm;
40
        $this->fileManager = $fileManager;
41
        $this->appRepository = $appRepository;
42
    }
43
44
    /**
45
     * @param string $id
46
     * @return object|null|ApplicationInterface
47
     */
48
    public function findApplication(string $id)
49
    {
50
        return $this->appRepository->find($id);
51
    }
52
53
    /**
54
     * @param string $appId
55
     * @param array $info
56
     * @return object|Attachment
57
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
58
     */
59
    public function handleAttachmentUpload(
60
        string $appId,
61
        array $info
62
    )
63
    {
64
        $application = $this->findApplication($appId);
65
        $attachment = $this->doUploadFile($application, $info);
0 ignored issues
show
Bug introduced by
It seems like $application can also be of type null; however, parameter $application of Applications\Service\UploadHandler::doUploadFile() does only seem to accept Applications\Entity\ApplicationInterface, 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

65
        $attachment = $this->doUploadFile(/** @scrutinizer ignore-type */ $application, $info);
Loading history...
66
        $dm = $this->dm;
67
68
        $application->getAttachments()->add($attachment);
69
        $dm->persist($application);
70
        $dm->flush();
71
72
        return $attachment;
73
    }
74
75
    /**
76
     * @param string $appID
77
     * @param array $info
78
     * @return object|ApplicationInterface
79
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
80
     */
81
    public function handleImageUpload(
82
        string $appID,
83
        array $info
84
    )
85
    {
86
        $dm = $this->dm;
87
        $application = $this->findApplication($appID);
88
        $attachment = $this->doUploadFile($application, $info);
0 ignored issues
show
Bug introduced by
It seems like $application can also be of type null; however, parameter $application of Applications\Service\UploadHandler::doUploadFile() does only seem to accept Applications\Entity\ApplicationInterface, 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

88
        $attachment = $this->doUploadFile(/** @scrutinizer ignore-type */ $application, $info);
Loading history...
89
90
        // remove existing image
91
        if(!is_null($application->getContact()->getImage())){
92
            $image = $application->getContact()->getImage();
93
            $application->getContact()->setImage(null);
94
            $dm->remove($image);
95
            $dm->persist($application);
96
            $dm->flush();
97
        }
98
99
        $application->getContact()->setImage($attachment);
100
        $dm->persist($application);
101
        $dm->flush();
102
103
        return $application;
104
    }
105
106
    private function doUploadFile(
107
        ApplicationInterface $application,
108
        array $info
109
    )
110
    {
111
        $fileManager = $this->fileManager;
112
        $user = $application->getUser();
113
114
        $metadata = new FileMetadata();
115
        $metadata->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of Core\Entity\FileMetadata::setUser() does only seem to accept Auth\Entity\UserInterface, 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

115
        $metadata->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
116
        $metadata->setContentType($info['type']);
117
        $metadata->setName($info['name']);
118
119
        $this->dm->persist($user);
120
        return $fileManager->uploadFromFile(
121
            Attachment::class,
122
            $metadata,
123
            $info['tmp_name'],
124
            $info['name']
125
        );
126
    }
127
}