DownloadOperation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Storeman\Operation;
4
5
use Storeman\FileReader;
6
use Storeman\VaultLayout\VaultLayoutInterface;
7
8
class DownloadOperation implements OperationInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $relativePath;
14
15
    /**
16
     * @var string
17
     */
18
    protected $blobId;
19
20
    public function __construct(string $relativePath, string $blobId)
21
    {
22
        $this->relativePath = $relativePath;
23
        $this->blobId = $blobId;
24
    }
25
26
    public function execute(string $localBasePath, FileReader $fileReader, VaultLayoutInterface $vaultLayout): bool
27
    {
28
        $localStream = fopen($localBasePath . $this->relativePath, 'wb');
29
        $remoteStream = $vaultLayout->readBlob($this->blobId);
30
31
        $bytesCopied = stream_copy_to_stream($remoteStream, $localStream);
32
33
        fclose($remoteStream);
34
        fclose($localStream);
35
36
        return $bytesCopied !== false;
37
    }
38
39
    /**
40
     * @codeCoverageIgnore
41
     */
42
    public function __toString(): string
43
    {
44
        return sprintf('Download %s (blobId %s)', $this->relativePath, $this->blobId);
45
    }
46
}
47