Passed
Push — master ( eae2c0...7dd8b7 )
by Julito
08:23
created

AssetRepository   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
dl 0
loc 137
rs 10
c 1
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getStorage() 0 3 1
A unZipFile() 0 22 5
A getFileSystem() 0 3 1
A getAssetUrl() 0 21 3
A getFolder() 0 9 2
A createFromRequest() 0 11 3
A getAssetContent() 0 14 3
A createFromString() 0 8 1
A delete() 0 5 2
A update() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
10
use Chamilo\CoreBundle\Entity\Asset;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Persistence\ManagerRegistry;
13
use League\Flysystem\FilesystemOperator;
14
use PhpZip\ZipFile;
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
use Symfony\Component\Routing\RouterInterface;
17
use Vich\UploaderBundle\Storage\FlysystemStorage;
18
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
19
20
class AssetRepository extends ServiceEntityRepository
21
{
22
    protected RouterInterface $router;
23
    protected UploaderHelper $uploaderHelper;
24
    protected FilesystemOperator $filesystem;
25
    protected FlysystemStorage $storage;
26
27
    public function __construct(ManagerRegistry $registry, RouterInterface $router, UploaderHelper $uploaderHelper, FilesystemOperator $assetFilesystem, FlysystemStorage $storage)
28
    {
29
        parent::__construct($registry, Asset::class);
30
        $this->router = $router;
31
        $this->uploaderHelper = $uploaderHelper;
32
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
33
        $this->filesystem = $assetFilesystem;
34
        $this->storage = $storage;
35
    }
36
37
    public function getStorage(): FlysystemStorage
38
    {
39
        return $this->storage;
40
    }
41
42
    public function getFileSystem(): FilesystemOperator
43
    {
44
        return $this->filesystem;
45
    }
46
47
    public function unZipFile(Asset $asset, ZipFile $zipFile): void
48
    {
49
        $folder = '/'.$asset->getCategory().'/'.$asset->getTitle();
50
51
        $fs = $this->getFileSystem();
52
53
        if ($fs->fileExists($folder)) {
54
            $list = $zipFile->getEntries();
55
            foreach ($list as $item) {
56
                $name = $item->getName();
57
                if ($fs->fileExists($folder.'/'.$name)) {
58
                    continue;
59
                }
60
61
                if ($item->isDirectory()) {
62
                    $fs->createDirectory($folder.'/'.$name);
63
64
                    continue;
65
                }
66
67
                $content = $zipFile->getEntryContents($name);
68
                $fs->write($folder.'/'.$name, $content);
69
            }
70
        }
71
    }
72
73
    public function getAssetContent(Asset $asset): string
74
    {
75
        if (!$asset->hasFile()) {
76
            return '';
77
        }
78
79
        $fs = $this->getFileSystem();
80
        $file = $this->getStorage()->resolveUri($asset);
81
82
        if ($fs->fileExists($file)) {
83
            return $this->getFileSystem()->read($file);
84
        }
85
86
        return '';
87
    }
88
89
    public function getFolder(Asset $asset): ?string
90
    {
91
        if ($asset->hasFile()) {
92
            $file = $asset->getTitle();
93
94
            return '/'.$asset->getCategory().'/'.$file.'/';
95
        }
96
97
        return null;
98
    }
99
100
    public function getAssetUrl(Asset $asset): string
101
    {
102
        if (Asset::SCORM === $asset->getCategory()) {
103
            $params = [
104
                'category' => $asset->getCategory(),
105
                'path' => $asset->getTitle(),
106
            ];
107
108
            return $this->router->generate('chamilo_core_asset_showfile', $params);
109
        }
110
111
        // Classic.
112
        $helper = $this->uploaderHelper;
113
114
        $cropFilter = '';
115
        $crop = $asset->getCrop();
116
        if (!empty($crop)) {
117
            $cropFilter = '?crop='.$crop;
118
        }
119
120
        return '/assets'.$helper->asset($asset).$cropFilter;
121
    }
122
123
    public function createFromRequest(Asset $asset, array $file): Asset
124
    {
125
        if (isset($file['tmp_name']) && !empty($file['tmp_name'])) {
126
            $mimeType = mime_content_type($file['tmp_name']);
127
            $file = new UploadedFile($file['tmp_name'], $asset->getTitle(), $mimeType, null, true);
128
            $asset->setFile($file);
129
            $this->getEntityManager()->persist($asset);
130
            $this->getEntityManager()->flush();
131
        }
132
133
        return $asset;
134
    }
135
136
    public function createFromString(Asset $asset, string $mimeType, string $content): Asset
137
    {
138
        $file = CreateUploadedFile::fromString($asset->getTitle(), $mimeType, $content);
139
        $asset->setFile($file);
140
        $this->getEntityManager()->persist($asset);
141
        $this->getEntityManager()->flush();
142
143
        return $asset;
144
    }
145
146
    public function update(Asset $asset): void
147
    {
148
        $this->getEntityManager()->persist($asset);
149
        $this->getEntityManager()->flush();
150
    }
151
152
    public function delete(Asset $asset = null): void
153
    {
154
        if (null !== $asset) {
155
            $this->getEntityManager()->remove($asset);
156
            $this->getEntityManager()->flush();
157
        }
158
    }
159
}
160