Passed
Push — master ( 960b9d...47eb91 )
by Kanstantsin
03:05
created

Saver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
ccs 25
cts 31
cp 0.8065
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 4 2
B save() 0 22 5
A __construct() 0 8 2
A isSaved() 0 4 2
A write() 0 9 4
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload\saver;
5
6
use League\Flysystem\FilesystemInterface;
7
use tkanstantsin\fileupload\config\InvalidConfigException;
8
use tkanstantsin\fileupload\formatter\File;
9
use tkanstantsin\fileupload\model\IFile;
10
11
/**
12
 * Class Saver allows store processed files.
13
 * E.g. store uploaded files or cache cropped/prepared images.
14
 *
15
 * @todo add option to force rewriting existed file.
16
 */
17
class Saver
18
{
19
    /**
20
     * @var IFile
21
     */
22
    public $file;
23
    /**
24
     * Filesystem where file will be stored
25
     * @var FilesystemInterface
26
     */
27
    public $filesystem;
28
    /**
29
     * File path in $filesystem
30
     * @var string
31
     */
32
    public $path;
33
34
    /**
35
     * Saver constructor.
36
     * @param IFile $file
37
     * @param FilesystemInterface $filesystem
38
     * @param string $path
39
     * @throws InvalidConfigException
40
     */
41 1
    public function __construct(IFile $file, FilesystemInterface $filesystem, string $path)
42
    {
43 1
        $this->file = $file;
44 1
        $this->filesystem = $filesystem;
45 1
        $this->path = $path;
46
47 1
        if ($this->path === '') {
48
            throw new InvalidConfigException('Saver path must be not empty');
49
        }
50 1
    }
51
52
    /**
53
     * Copies, processes and saves file in $filesystem
54
     * @param \tkanstantsin\fileupload\formatter\File $formatter
55
     * @return bool
56
     * @throws \InvalidArgumentException
57
     * @throws \League\Flysystem\FileNotFoundException
58
     */
59 1
    public function save(File $formatter): bool
60
    {
61 1
        $isCached = $this->isSaved();
62 1
        $isEmpty = $this->isEmpty();
63 1
        if ($isCached && !$isEmpty) {
64
            return true;
65
        }
66 1
        if ($isEmpty) {
67
            return false;
68
        }
69
70
        // checks if path is writable
71
        // create new empty file or override existed one
72
        // also caches empty result for non-formatted files
73 1
        $this->filesystem->put($this->path, null);
74
75 1
        $content = $formatter->getContent();
76 1
        if ($content === '') {
77
            return false;
78
        }
79
80 1
        return $this->write($content);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 1
    protected function isEmpty(): bool
87
    {
88 1
        return $this->filesystem->has($this->path)
89 1
            && $this->filesystem->getSize($this->path) === 0;
90
    }
91
92
    /**
93
     * Checks if file is already in $path.
94
     * @return bool
95
     * @throws \League\Flysystem\FileNotFoundException
96
     */
97 1
    protected function isSaved(): bool
98
    {
99 1
        return $this->filesystem->has($this->path)
100 1
            && $this->filesystem->getTimestamp($this->path) > $this->file->getUpdatedAt();
101
    }
102
103
    /**
104
     * Saves file into $filesystem
105
     * @param $content
106
     * @return bool
107
     * @throws \InvalidArgumentException
108
     */
109 1
    protected function write($content): bool
110
    {
111 1
        if ($content === false || $content === null) {
112
            return false;
113
        }
114
115 1
        return \is_resource($content)
116
            ? $this->filesystem->putStream($this->path, $content)
117 1
            : $this->filesystem->put($this->path, $content);
118
    }
119
}