Completed
Push — master ( 77f170...166d1e )
by Arne
03:02
created

UploadOperation::execute()   B

Complexity

Conditions 3
Paths 6

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 11
nc 6
nop 2
1
<?php
2
3
namespace Archivr\Operation;
4
5
use Archivr\ConnectionAdapter\ConnectionAdapterInterface;
6
use Archivr\Exception\Exception;
7
8
class UploadOperation implements OperationInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $relativePath;
14
15
    /**
16
     * @var string
17
     */
18
    protected $blobId;
19
20
    /**
21
     * @var array
22
     */
23
    protected $streamFilterConfigMap;
24
25
    public function __construct(string $relativePath, string $blobId, array $streamFilterConfigMap = [])
26
    {
27
        $this->relativePath = $relativePath;
28
        $this->blobId = $blobId;
29
        $this->streamFilterConfigMap = $streamFilterConfigMap;
30
    }
31
32
    public function execute(string $localBasePath, ConnectionAdapterInterface $connection): bool
33
    {
34
        $absolutePath = $localBasePath . $this->relativePath;
35
36
        $localStream = fopen($absolutePath, 'rb');
37
38
        foreach ($this->streamFilterConfigMap as $filterName => $filterParams)
39
        {
40
            stream_filter_append($localStream, $filterName, STREAM_FILTER_READ, $filterParams);
41
        }
42
43
        try
44
        {
45
            $connection->writeStream($this->blobId, $localStream);
46
47
            fclose($localStream);
48
49
            return true;
50
        }
51
        catch (Exception $exception)
52
        {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * @codeCoverageIgnore
59
     */
60
    public function __toString(): string
61
    {
62
        $filterNames = implode(',', array_keys($this->streamFilterConfigMap)) ?: '-';
63
64
        return sprintf('Upload %s (blobId %s, filters: %s)', $this->relativePath, $this->blobId, $filterNames);
65
    }
66
}
67