Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

MediaManager::getProperObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.0078
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\MediaFactoryInterface;
19
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
20
use SWP\Bundle\ContentBundle\Model\FileInterface;
21
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
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 MediaFactoryInterface
30
     */
31
    protected $mediaFactory;
32
33
    /**
34
     * @var Filesystem
35
     */
36
    protected $filesystem;
37
38
    /**
39
     * @var RouterInterface
40
     */
41
    protected $router;
42
43
    /**
44
     * @var TenantContextInterface
45
     */
46
    protected $tenantContext;
47
48
    /**
49
     * @var ArticleMediaRepositoryInterface
50
     */
51
    protected $mediaRepository;
52
53
    /**
54
     * MediaManager constructor.
55
     *
56
     * @param ArticleMediaRepositoryInterface $mediaRepository
57
     * @param MediaFactoryInterface           $mediaFactory
58
     * @param Filesystem                      $filesystem
59
     * @param RouterInterface                 $router
60
     * @param TenantContextInterface          $tenantContext
61
     */
62 22
    public function __construct(
63
        ArticleMediaRepositoryInterface $mediaRepository,
64
        MediaFactoryInterface $mediaFactory,
65
        Filesystem $filesystem,
66
        RouterInterface $router,
67
        TenantContextInterface $tenantContext
68
    ) {
69 22
        $this->mediaRepository = $mediaRepository;
70 22
        $this->mediaFactory = $mediaFactory;
71 22
        $this->filesystem = $filesystem;
72 22
        $this->router = $router;
73 22
        $this->tenantContext = $tenantContext;
74 22
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 9
    public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId)
80
    {
81 9
        $mediaId = ArticleMedia::handleMediaId($mediaId);
82 9
        $this->saveFile($uploadedFile, $mediaId);
83
84 9
        $asset = $this->mediaFactory->createMediaAsset($uploadedFile, $mediaId);
85 9
        $this->mediaRepository->add($asset);
86
87 9
        return $asset;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 4
    public function getFile(FileInterface $media)
94
    {
95 4
        return $this->filesystem->read($this->getMediaBasePath().'/'.$media->getAssetId().'.'.$media->getFileExtension());
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 9
    public function saveFile(UploadedFile $uploadedFile, $fileName)
102
    {
103 9
        $stream = fopen($uploadedFile->getRealPath(), 'r+');
104 9
        $result = $this->filesystem->writeStream($this->getMediaBasePath().'/'.$fileName.'.'.$uploadedFile->guessClientExtension(), $stream);
105 9
        fclose($stream);
106
107 9
        return $result;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 4
    public function getMediaPublicUrl(FileInterface $media)
114
    {
115 4
        $tenant = $this->tenantContext->getTenant();
116
117 4
        if ($subdomain = $tenant->getSubdomain()) {
118 4
            $context = $this->router->getContext();
119 4
            $context->setHost($subdomain.'.'.$context->getHost());
120
        }
121
122 4
        return $this->getMediaUri($media, RouterInterface::ABSOLUTE_URL);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 5
    public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH)
129
    {
130 5
        return $this->router->generate('swp_media_get', [
131 5
            'mediaId' => $media->getAssetId(),
132 5
            'extension' => $media->getFileExtension(),
133
        ], $type);
134
    }
135
136 9
    protected function getMediaBasePath(): string
137
    {
138 9
        $tenant = $this->tenantContext->getTenant();
139 9
        $pathElements = ['swp', $tenant->getOrganization()->getCode(), $tenant->getCode(), 'media'];
140
141 9
        return implode('/', $pathElements);
142
    }
143
}
144