Completed
Pull Request — 1.4 (#705)
by Paweł
08:56
created

MediaManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 9
dl 0
loc 139
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMediaPublicUrl() 0 4 1
A createMediaAsset() 0 6 1
A getMediaBasePath() 0 6 1
A guessExtension() 0 10 3
A getMediaUri() 0 7 1
A __construct() 0 11 1
A handleUploadedFile() 0 9 1
A getFile() 0 4 1
A saveFile() 0 15 2
A downloadFile() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Manager;
16
17
use Hoa\Mime\Mime;
18
use SWP\Bundle\ContentBundle\Doctrine\ArticleMediaRepositoryInterface;
19
use SWP\Bundle\ContentBundle\Factory\FileFactoryInterface;
20
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
21
use SWP\Bundle\ContentBundle\Model\FileInterface;
22
use Symfony\Component\HttpFoundation\File\UploadedFile;
23
use League\Flysystem\Filesystem;
24
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
25
use Symfony\Component\Routing\RouterInterface;
26
27
class MediaManager implements MediaManagerInterface
28
{
29
    /**
30
     * @var Filesystem
31
     */
32
    protected $filesystem;
33
34
    /**
35
     * @var RouterInterface
36
     */
37
    protected $router;
38
39
    /**
40
     * @var ArticleMediaRepositoryInterface
41
     */
42
    protected $mediaRepository;
43
44
    /**
45
     * @var FileFactoryInterface
46
     */
47
    protected $fileFactory;
48
49
    public function __construct(
50
        ArticleMediaRepositoryInterface $mediaRepository,
51
        Filesystem $filesystem,
52
        RouterInterface $router,
53
        FileFactoryInterface $fileFactory
54
    ) {
55
        $this->mediaRepository = $mediaRepository;
56
        $this->filesystem = $filesystem;
57
        $this->router = $router;
58
        $this->fileFactory = $fileFactory;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId)
65
    {
66
        $mediaId = ArticleMedia::handleMediaId($mediaId);
67
        $asset = $this->createMediaAsset($uploadedFile, $mediaId);
68
        $this->saveFile($uploadedFile, $mediaId);
69
        $this->mediaRepository->persist($asset);
70
71
        return $asset;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getFile(FileInterface $media)
78
    {
79
        return $this->filesystem->read($this->getMediaBasePath().'/'.$media->getAssetId().'.'.$media->getFileExtension());
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function saveFile(UploadedFile $uploadedFile, $fileName)
86
    {
87
        $extension = $this->guessExtension($uploadedFile);
88
        $filePath = $this->getMediaBasePath().'/'.$fileName.'.'.$extension;
89
90
        if ($this->filesystem->has($filePath)) {
91
            return true;
92
        }
93
94
        $stream = fopen($uploadedFile->getRealPath(), 'r+');
95
        $result = $this->filesystem->writeStream($filePath, $stream);
96
        fclose($stream);
97
98
        return $result;
99
    }
100
101
    public function downloadFile(string $url, string $mediaId, string $mimeType = null): UploadedFile
102
    {
103
        $pathParts = \pathinfo($url);
104
        if (null === $mimeType) {
105
            $mimeType = Mime::getMimeFromExtension($pathParts['extension']);
106
        }
107
108
        $file = \file_get_contents($url);
109
        $tempLocation = \sys_get_temp_dir().\DIRECTORY_SEPARATOR.\sha1($mediaId.date('his'));
110
        $filesystem = new SymfonyFilesystem();
111
        $filesystem->dumpFile($tempLocation, $file);
112
113
        return new UploadedFile($tempLocation, $mediaId, $mimeType, \strlen($file), null, true);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getMediaPublicUrl(FileInterface $media)
120
    {
121
        return $this->getMediaUri($media, RouterInterface::ABSOLUTE_URL);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH)
128
    {
129
        return $this->router->generate('swp_media_get', [
130
            'mediaId' => $media->getAssetId(),
131
            'extension' => $media->getFileExtension(),
132
        ], $type);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function createMediaAsset(UploadedFile $uploadedFile, string $assetId): FileInterface
139
    {
140
        $extension = $this->guessExtension($uploadedFile);
141
142
        return $this->fileFactory->createWith($assetId, $extension);
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    protected function getMediaBasePath(): string
149
    {
150
        $pathElements = ['swp', 'media'];
151
152
        return implode('/', $pathElements);
153
    }
154
155
    private function guessExtension(UploadedFile $uploadedFile): string
156
    {
157
        $extension = $uploadedFile->guessExtension();
158
159
        if ('mpga' === $extension && 'mp3' === $uploadedFile->getExtension()) {
160
            $extension = 'mp3';
161
        }
162
163
        return $extension;
164
    }
165
}
166