Completed
Push — master ( 3263c7...ec87e5 )
by Nelson
04:25
created

Filesystem   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A store() 0 14 3
A retrieve() 0 10 3
A getUrl() 0 3 1
A getDocPath() 0 4 1
1
<?php
2
3
namespace DocumentStorage\Adapter\Storage;
4
5
use DocumentStorage\Storage;
6
use DocumentStorage\Exception\DocumentNotFoundException;
7
use DocumentStorage\Exception\DocumentNotStoredException;
8
9
class Filesystem implements Storage
10
{
11
    /** @var string */
12
    private $storageDir;
13
14
    /**
15
     * @throws \InvalidArgumentException If $storageDir is not a directory
16
     * @throws \InvalidArgumentException If $storageDir is not writable
17
     */
18
    public function __construct(string $storageDir)
19
    {
20
        if (!is_dir($storageDir)) {
21
            throw new \InvalidArgumentException(sprintf('[%s] is not a directory', $storageDir));
22
        }
23
24
        if (!is_writable($storageDir)) {
25
            throw new \InvalidArgumentException(sprintf('[%s] is not writable', $storageDir));
26
        }
27
28
        $this->storageDir = $storageDir;
29
    }
30
31
    public function store(string $pathOrBody, string $docName, string $oldDocName = '') : string
32
    {
33
        $docPath = $this->getDocPath($docName);
34
35
        $storage = file_exists($pathOrBody)
36
                 ? copy($pathOrBody, $docPath)
37
                 : file_put_contents($docPath, $pathOrBody);
38
39
        if (false === $storage) {
40
            throw new DocumentNotStoredException('There was an error storing the document [%s] to the filesystem.');
41
        }
42
43
        return $docPath;
44
    }
45
46
    public function retrieve(string $docName) : string
47
    {
48
        $docPath = $this->getDocPath($docName);
49
50
        if (false === file_exists($docPath) || false === $contents = file_get_contents($docPath)) {
51
            throw new DocumentNotFoundException(sprintf('Could not retrieve [%s]', $docPath));
52
        }
53
54
        return $contents;
55
    }
56
57
    public function getUrl(string $docName) : string
58
    {
59
    }
60
61
    private function getDocPath(string $docName) : string
62
    {
63
        return $this->storageDir.DIRECTORY_SEPARATOR.$docName;
64
    }
65
}
66