Completed
Push — master ( 6da6df...dc6a94 )
by Rafał
09:28
created

MediaManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 5 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 21.43%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 7
dl 7
loc 140
ccs 6
cts 28
cp 0.2143
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A handleUploadedFile() 0 9 1
A getFile() 0 4 1
A saveFile() 0 15 2
A getMediaPublicUrl() 0 4 1
A getMediaUri() 7 7 1
A createMediaAsset() 0 6 1
A getMediaBasePath() 0 6 1
A guessExtension() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Component\Storage\Factory\FactoryInterface;
22
use Symfony\Component\HttpFoundation\File\UploadedFile;
23
use League\Flysystem\Filesystem;
24
use Symfony\Component\Routing\RouterInterface;
25
26
class MediaManager implements MediaManagerInterface
27
{
28
    /**
29
     * @var Filesystem
30
     */
31
    protected $filesystem;
32
33
    /**
34
     * @var RouterInterface
35
     */
36
    protected $router;
37
38
    /**
39
     * @var ArticleMediaRepositoryInterface
40
     */
41
    protected $mediaRepository;
42
43
    /**
44
     * @var FactoryInterface
45
     */
46
    protected $imageFactory;
47
48
    /**
49
     * @var FileFactoryInterface
50
     */
51
    protected $fileFactory;
52
53
    /**
54
     * MediaManager constructor.
55 12
     *
56
     * @param ArticleMediaRepositoryInterface $mediaRepository
57
     * @param Filesystem                      $filesystem
58
     * @param RouterInterface                 $router
59
     * @param FactoryInterface                $imageFactory
60
     * @param FileFactoryInterface            $fileFactory
61 12
     */
62 12
    public function __construct(
63 12
        ArticleMediaRepositoryInterface $mediaRepository,
64 12
        Filesystem $filesystem,
65 12
        RouterInterface $router,
66
        FactoryInterface $imageFactory,
67
        FileFactoryInterface $fileFactory
68
    ) {
69
        $this->mediaRepository = $mediaRepository;
70
        $this->filesystem = $filesystem;
71
        $this->router = $router;
72
        $this->imageFactory = $imageFactory;
73
        $this->fileFactory = $fileFactory;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId)
80
    {
81
        $mediaId = ArticleMedia::handleMediaId($mediaId);
82
        $this->saveFile($uploadedFile, $mediaId);
83
        $asset = $this->createMediaAsset($uploadedFile, $mediaId);
84
        $this->mediaRepository->persist($asset);
85
86
        return $asset;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getFile(FileInterface $media)
93
    {
94
        return $this->filesystem->read($this->getMediaBasePath().'/'.$media->getAssetId().'.'.$media->getFileExtension());
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function saveFile(UploadedFile $uploadedFile, $fileName)
101
    {
102
        $extension = $this->guessExtension($uploadedFile);
103
        $filePath = $this->getMediaBasePath().'/'.$fileName.'.'.$extension;
104
105
        if ($this->filesystem->has($filePath)) {
106
            return true;
107
        }
108
109
        $stream = fopen($uploadedFile->getRealPath(), 'r+');
110
        $result = $this->filesystem->writeStream($filePath, $stream);
111
        fclose($stream);
112
113
        return $result;
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 View Code Duplication
    public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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