Passed
Push — master ( 8c8eae...511b66 )
by Julito
11:06
created

AssetRepository::unZipFile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 2
dl 0
loc 22
rs 9.5222
c 0
b 0
f 0
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\Entity\Asset;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Persistence\ManagerRegistry;
12
use League\Flysystem\FilesystemOperator;
13
use PhpZip\ZipFile;
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
use Symfony\Component\Routing\RouterInterface;
16
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
17
18
class AssetRepository extends ServiceEntityRepository
19
{
20
    protected RouterInterface $router;
21
    protected UploaderHelper $uploaderHelper;
22
    protected FilesystemOperator $filesystem;
23
24
    public function __construct(ManagerRegistry $registry, RouterInterface $router, UploaderHelper $uploaderHelper, FilesystemOperator $assetFilesystem)
25
    {
26
        parent::__construct($registry, Asset::class);
27
        $this->router = $router;
28
        $this->uploaderHelper = $uploaderHelper;
29
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
30
        $this->filesystem = $assetFilesystem;
31
    }
32
33
    public function getFileSystem()
34
    {
35
        return $this->filesystem;
36
    }
37
38
    /*public function getUploaderHelper(): UploaderHelper
39
    {
40
        return $this->uploaderHelper;
41
    }*/
42
43
    public function unZipFile(Asset $asset, ZipFile $zipFile): void
44
    {
45
        $folder = '/'.$asset->getCategory().'/'.$asset->getTitle();
46
47
        $fs = $this->getFileSystem();
48
49
        if ($fs->fileExists($folder)) {
50
            $list = $zipFile->getEntries();
51
            foreach ($list as $item) {
52
                $name = $item->getName();
53
                if ($fs->fileExists($folder.'/'.$name)) {
54
                    continue;
55
                }
56
57
                if ($item->isDirectory()) {
58
                    $fs->createDirectory($folder.'/'.$name);
59
60
                    continue;
61
                }
62
63
                $content = $zipFile->getEntryContents($name);
64
                $fs->write($folder.'/'.$name, $content);
65
            }
66
        }
67
    }
68
69
    public function getFolder(Asset $asset): ?string
70
    {
71
        if ($asset->hasFile()) {
72
            $file = $asset->getTitle();
73
74
            return '/'.$asset->getCategory().'/'.$file.'/';
75
        }
76
77
        return null;
78
    }
79
80
    public function getAssetUrl(Asset $asset)
81
    {
82
        if (Asset::SCORM === $asset->getCategory()) {
83
            return $this->router->generate(
84
                'chamilo_core_asset_showfile',
85
                [
86
                    'category' => $asset->getCategory(),
87
                    'path' => $asset->getTitle(),
88
                ]
89
            );
90
        }
91
92
        // Classic.
93
        $helper = $this->uploaderHelper;
94
95
        return '/assets'.$helper->asset($asset);
96
    }
97
98
    public function createFromRequest(Asset $asset, $file): Asset
99
    {
100
        if (isset($file['tmp_name']) && !empty($file['tmp_name'])) {
101
            $mimeType = mime_content_type($file['tmp_name']);
102
            $file = new UploadedFile($file['tmp_name'], $asset->getTitle(), $mimeType, null, true);
103
            $asset->setFile($file);
104
            $this->getEntityManager()->persist($asset);
105
            $this->getEntityManager()->flush();
106
        }
107
108
        return $asset;
109
    }
110
111
    /*public function getFileContent(Asset $asset): string
112
    {
113
        try {
114
            if ($asset->hasFile()) {
115
                $file = $asset->getFile();
116
                $fileName = $this->getFilename($file);
117
118
                return $this->getFileSystem()->read($fileName);
119
            }
120
121
            return '';
122
        } catch (\Throwable $exception) {
123
            throw new FileNotFoundException($asset);
124
        }
125
    }*/
126
127
    /*public function getFileStream(Asset $asset)
128
    {
129
        try {
130
            if ($asset->hasFile()) {
131
                $file = $asset->getFile();
132
                $fileName = $this->getFilename($file);
133
134
                return $this->getFileSystem()->readStream($fileName);
135
            }
136
137
            return '';
138
        } catch (\Throwable $exception) {
139
            throw new FileNotFoundException($asset);
140
        }
141
    }*/
142
143
    public function delete(Asset $asset = null): void
144
    {
145
        if (null !== $asset) {
146
            $this->getEntityManager()->remove($asset);
147
            $this->getEntityManager()->flush();
148
        }
149
    }
150
}
151