Completed
Push — master ( 2cde75...6c52d6 )
by Arne
02:04
created

DownloadOperation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A execute() 0 17 2
A __toString() 0 6 2
1
<?php
2
3
namespace Storeman\Operation;
4
5
use Storeman\StorageAdapter\StorageAdapterInterface;
6
7
class DownloadOperation implements OperationInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $relativePath;
13
14
    /**
15
     * @var string
16
     */
17
    protected $blobId;
18
19
    /**
20
     * @var array
21
     */
22
    protected $streamFilterConfigMap;
23
24
    public function __construct(string $relativePath, string $blobId, array $streamFilterConfigMap = [])
25
    {
26
        $this->relativePath = $relativePath;
27
        $this->blobId = $blobId;
28
        $this->streamFilterConfigMap = $streamFilterConfigMap;
29
    }
30
31
    public function execute(string $localBasePath, StorageAdapterInterface $storageAdapter): bool
32
    {
33
        $localStream = fopen($localBasePath . $this->relativePath, 'wb');
34
        $remoteStream = $storageAdapter->getReadStream($this->blobId);
35
36
        foreach ($this->streamFilterConfigMap as $filterName => $filterParams)
37
        {
38
            stream_filter_append($remoteStream, $filterName, STREAM_FILTER_READ, $filterParams);
39
        }
40
41
        $bytesCopied = stream_copy_to_stream($remoteStream, $localStream);
42
43
        fclose($remoteStream);
44
        fclose($localStream);
45
46
        return $bytesCopied !== false;
47
    }
48
49
    /**
50
     * @codeCoverageIgnore
51
     */
52
    public function __toString(): string
53
    {
54
        $filterNames = implode(',', array_keys($this->streamFilterConfigMap)) ?: '-';
55
56
        return sprintf('Download %s (blobId %s, filters: %s)', $this->relativePath, $this->blobId, $filterNames);
57
    }
58
}
59