Passed
Push — master ( 240c83...03f39b )
by Mathias
06:41
created

FileManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
eloc 40
dl 0
loc 117
rs 10
c 1
b 0
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 5 1
A __construct() 0 7 1
A findByID() 0 4 1
A getContents() 0 5 1
A getStream() 0 4 1
A getRepository() 0 3 1
A remove() 0 11 3
A uploadFromStream() 0 7 1
A uploadFromFile() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Core\Service;
6
7
8
use Auth\AuthenticationService;
9
use Auth\Entity\AnonymousUser;
10
use Auth\Entity\UserImage;
11
use Core\Entity\FileInterface;
12
use Core\Entity\FileMetadataInterface;
13
use Core\Entity\ImageInterface;
14
use Core\Entity\PermissionsInterface;
15
use Doctrine\ODM\MongoDB\DocumentManager;
16
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
17
use Imagine\Image\ImageInterface as ImagineImage;
18
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
19
use Doctrine\ODM\MongoDB\Repository\UploadOptions;
20
use Doctrine\Persistence\ObjectRepository;
21
use Imagine\Image\ImagineInterface;
22
use Psr\Container\ContainerInterface;
23
24
class FileManager
25
{
26
    /**
27
     * @var DocumentManager
28
     */
29
    private DocumentManager $dm;
30
31
    /**
32
     * @var AuthenticationService
33
     */
34
    private AuthenticationService $auth;
35
36
    public function __construct(
37
        DocumentManager $dm,
38
        AuthenticationService $auth
39
    )
40
    {
41
        $this->dm = $dm;
42
        $this->auth = $auth;
43
    }
44
45
    public static function factory(ContainerInterface $container): self
46
    {
47
        $dm = $container->get(DocumentManager::class);
48
        $auth = $container->get('AuthenticationService');
49
        return new FileManager($dm, $auth);
50
    }
51
52
    /**
53
     * @param string $entityClass
54
     * @param string $id
55
     * @return object|null|FileInterface
56
     */
57
    public function findByID(string $entityClass, string $id)
58
    {
59
        $repo = $this->getRepository($entityClass);
60
        return $repo->find($id);
61
    }
62
63
    /**
64
     * @param FileInterface $file
65
     * @return resource
66
     */
67
    public function getStream(FileInterface $file)
68
    {
69
        $repo = $this->getRepository(get_class($file));
70
        return $repo->openDownloadStream($file->getId());
0 ignored issues
show
Bug introduced by
The method openDownloadStream() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ODM\MongoDB\Repository\GridFSRepository or Doctrine\ODM\MongoDB\Rep...DefaultGridFSRepository. ( Ignorable by Annotation )

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

70
        return $repo->/** @scrutinizer ignore-call */ openDownloadStream($file->getId());
Loading history...
71
    }
72
73
    public function getContents(FileInterface $file): string
74
    {
75
        $repo = $this->getRepository(get_class($file));
76
        $stream = $repo->openDownloadStream($file->getId());
77
        return stream_get_contents($stream);
78
    }
79
80
    public function uploadFromFile(string $entityClass, FileMetadataInterface $metadata, string $source, ?string $fileName = null): object
81
    {
82
        $repo = $this->getRepository($entityClass);
83
84
        if(UserImage::class !== $entityClass && is_null($metadata->getUser())){
85
            $user = $this->auth->getUser();
86
87
            if($user instanceof AnonymousUser){
0 ignored issues
show
introduced by
$user is always a sub-type of Auth\Entity\AnonymousUser.
Loading history...
88
                $metadata->getPermissions()->grant($user, PermissionsInterface::PERMISSION_ALL);
89
            }else{
90
                $this->dm->getUnitOfWork()->registerManaged($user, $user->getId(), []);
91
92
                $metadata->setUser($user);
93
                $this->dm->persist($user);
94
                $this->dm->flush();
95
            }
96
        }
97
98
        $options = new UploadOptions();
99
        $options->metadata = $metadata;
100
101
        return $repo->uploadFromFile($source, $fileName, $options);
0 ignored issues
show
Bug introduced by
The method uploadFromFile() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ODM\MongoDB\Repository\GridFSRepository or Doctrine\ODM\MongoDB\Rep...DefaultGridFSRepository. ( Ignorable by Annotation )

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

101
        return $repo->/** @scrutinizer ignore-call */ uploadFromFile($source, $fileName, $options);
Loading history...
102
    }
103
104
    /**
105
     * @param string $entityClass
106
     * @param FileMetadataInterface $metadata
107
     * @param string $fileName
108
     * @param resource $stream
109
     * @return object|FileInterface|ImageInterface
110
     */
111
    public function uploadFromStream(string $entityClass, FileMetadataInterface $metadata, string $fileName, $stream): object
112
    {
113
        $repo = $this->getRepository($entityClass);
114
        $options = new UploadOptions();
115
        $options->metadata = $metadata;
116
117
        return $repo->uploadFromStream($fileName, $stream, $options);
0 ignored issues
show
Bug introduced by
The method uploadFromStream() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ODM\MongoDB\Repository\GridFSRepository or Doctrine\ODM\MongoDB\Rep...DefaultGridFSRepository. ( Ignorable by Annotation )

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

117
        return $repo->/** @scrutinizer ignore-call */ uploadFromStream($fileName, $stream, $options);
Loading history...
118
    }
119
120
    public function remove(FileInterface $file, $andFlush = false)
121
    {
122
        $dm = $this->dm;
123
        $events = $dm->getEventManager();
124
125
        $dm->remove($file);
126
        $events->hasListeners('postRemoveEntity') &&
127
            $events->dispatchEvent('postRemoveEntity', new LifecycleEventArgs($file, $dm));
128
129
        if($andFlush){
130
            $dm->flush();
131
        }
132
    }
133
134
    /**
135
     * @param string $entityClass
136
     * @return ObjectRepository|GridFSRepository
137
     */
138
    private function getRepository(string $entityClass)
139
    {
140
        return $this->dm->getRepository($entityClass);
141
    }
142
}