Completed
Push — master ( 7ee0d9...1aff7f )
by Rafał
24:06 queued 15:30
created

ReportFileUploader   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A upload() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2020 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\AnalyticsExport;
18
19
use League\Flysystem\Filesystem;
20
use SWP\Bundle\CoreBundle\Model\AnalyticsReportInterface;
21
use Symfony\Component\Routing\RouterInterface;
22
23
final class ReportFileUploader
24
{
25
    /** @var CsvReportFileLocationResolver */
26
    private $assetLocationResolver;
27
28
    /** @var Filesystem */
29
    private $filesystem;
30
31
    /** @var RouterInterface */
32
    private $router;
33
34
    public function __construct(
35
        CsvReportFileLocationResolver $assetLocationResolver,
36
        Filesystem $filesystem,
37
        RouterInterface $router
38
    ) {
39
        $this->assetLocationResolver = $assetLocationResolver;
40
        $this->filesystem = $filesystem;
41
        $this->router = $router;
42
    }
43
44
    public function upload(AnalyticsReportInterface $file, string $sourcePath): string
45
    {
46
        $uploadPath = $this->assetLocationResolver->getMediaBasePath().'/'.$file->getAssetId();
47
48
        $stream = fopen($sourcePath, 'rb+');
49
        $this->filesystem->writeStream($uploadPath, $stream);
50
        fclose($stream);
51
52
        return $this->router->generate('swp_export_analytics_download', [
53
            'fileName' => $file->getAssetId(),
54
        ], RouterInterface::ABSOLUTE_URL);
55
    }
56
}
57