Completed
Pull Request — master (#835)
by Paweł
11:45
created

MediaManager::retryDecider()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 0
cp 0
rs 8.3466
c 0
b 0
f 0
cc 7
nc 1
nop 0
crap 56
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)
54
    {
55 12
        $mediaId = ArticleMedia::handleMediaId($mediaId);
56
        $asset = $this->createMediaAsset($uploadedFile, $mediaId);
57
        $this->saveFile($uploadedFile, $mediaId);
58
        $this->mediaRepository->persist($asset);
59
60
        return $asset;
61 12
    }
62 12
63 12
    public function getFile(FileInterface $media)
64 12
    {
65 12
        return $this->filesystem->read($this->assetLocationResolver->getMediaBasePath().'/'.$media->getAssetId().'.'.$media->getFileExtension());
66
    }
67
68
    public function saveFile(UploadedFile $uploadedFile, $fileName): bool
69
    {
70
        $filePath = $this->assetLocationResolver->getMediaBasePath().'/'.$fileName.'.'.$this->guessExtension($uploadedFile);
71
72
        if ($this->filesystem->has($filePath)) {
73
            return true;
74
        }
75
76
        $stream = fopen($uploadedFile->getRealPath(), 'r+');
77
        $result = $this->filesystem->writeStream($filePath, $stream);
78
        fclose($stream);
79
80
        return $result;
81
    }
82
83
    public function getMediaPublicUrl(FileInterface $media): string
84
    {
85
        return $this->getMediaUri($media, RouterInterface::ABSOLUTE_URL);
86
    }
87
88
    public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH): string
89
    {
90
        $uri = '/'.$this->assetLocationResolver->getAssetUrl($media);
91
        if (RouterInterface::ABSOLUTE_URL === $type) {
92
            $requestContext = $this->router->getContext();
93
            $uri = $requestContext->getScheme().'://'.$requestContext->getHost().$uri;
94
        }
95
96
        return $uri;
97
    }
98
99
    public function createMediaAsset(UploadedFile $uploadedFile, string $assetId): FileInterface
100
    {
101
        return $this->fileFactory->createWith($assetId, $this->guessExtension($uploadedFile));
102
    }
103
104
    private function guessExtension(UploadedFile $uploadedFile): string
105
    {
106
        $extension = $uploadedFile->guessExtension();
107
        if ('mpga' === $extension && 'mp3' === $uploadedFile->getClientOriginalExtension()) {
108
            $extension = 'mp3';
109
        }
110
111
        return $extension;
112
    }
113
}
114