Completed
Push — master ( 178130...678bac )
by Paweł
15:27
created

MediaManager::createMediaAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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 SWP\Bundle\ContentBundle\Doctrine\ArticleMediaRepositoryInterface;
18
use SWP\Bundle\ContentBundle\Factory\FileFactoryInterface;
19
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
20
use SWP\Bundle\ContentBundle\Model\FileInterface;
21
use SWP\Bundle\ContentBundle\Resolver\AssetLocationResolverInterface;
22
use Symfony\Bundle\FrameworkBundle\Routing\Router;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
use League\Flysystem\Filesystem;
25
use Symfony\Component\Routing\RouterInterface;
26
27
class MediaManager implements MediaManagerInterface
28
{
29
    protected $filesystem;
30
31
    protected $router;
32
33
    protected $mediaRepository;
34
35
    protected $fileFactory;
36
37
    protected $assetLocationResolver;
38
39
    public function __construct(
40
        ArticleMediaRepositoryInterface $mediaRepository,
41
        Filesystem $filesystem,
42
        Router $router,
43
        FileFactoryInterface $fileFactory,
44
        AssetLocationResolverInterface $assetLocationResolver
45
    ) {
46
        $this->mediaRepository = $mediaRepository;
47
        $this->filesystem = $filesystem;
48
        $this->router = $router;
49
        $this->fileFactory = $fileFactory;
50
        $this->assetLocationResolver = $assetLocationResolver;
51
    }
52
53
    public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId): FileInterface
54
    {
55 12
        $mediaId = ArticleMedia::handleMediaId($mediaId);
56
        $asset = $this->fileFactory->createWith($mediaId, $this->guessExtension($uploadedFile));
57
        $this->mediaRepository->persist($asset);
58
        $this->saveFile($uploadedFile, $mediaId);
59
60
        return $asset;
61 12
    }
62 12
63 12
    public function getFile(FileInterface $asset)
64 12
    {
65 12
        return $this->filesystem->read($this->assetLocationResolver->getMediaBasePath().'/'.$asset->getAssetId().'.'.$asset->getFileExtension());
66
    }
67
68
    public function getMediaPublicUrl(FileInterface $media): string
69
    {
70
        return $this->getMediaUri($media, RouterInterface::ABSOLUTE_URL);
71
    }
72
73
    public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH): string
74
    {
75
        $uri = '/'.$this->assetLocationResolver->getAssetUrl($media);
76
        if (RouterInterface::ABSOLUTE_URL === $type) {
77
            $requestContext = $this->router->getContext();
78
            $uri = $requestContext->getScheme().'://'.$requestContext->getHost().$uri;
79
        }
80
81
        return $uri;
82
    }
83
84
    public function saveFile(UploadedFile $uploadedFile, $fileName): void
85
    {
86
        $filePath = $this->assetLocationResolver->getMediaBasePath().'/'.$fileName.'.'.$this->guessExtension($uploadedFile);
87
        if ($this->filesystem->has($filePath)) {
88
            return;
89
        }
90
91
        $stream = fopen($uploadedFile->getRealPath(), 'rb+');
92
        $this->filesystem->writeStream($filePath, $stream);
93
        fclose($stream);
94
    }
95
96
    private function guessExtension(UploadedFile $uploadedFile): string
97
    {
98
        $extension = $uploadedFile->guessExtension();
99
        if ('mpga' === $extension && 'mp3' === $uploadedFile->getClientOriginalExtension()) {
100
            $extension = 'mp3';
101
        }
102
103
        return $extension;
104
    }
105
}
106