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

UploadOperation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 24 3
A __toString() 0 6 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