Passed
Push — master ( dbb7ba...c6992b )
by Kanstantsin
04:38
created

Saver::save()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.288

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 5
nop 1
dl 0
loc 28
ccs 12
cts 15
cp 0.8
crap 6.288
rs 8.439
c 0
b 0
f 0
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
        $saved = $this->write($content);
81
82 1
        if ($saved) {
83 1
            $formatter->afterCacheCallback();
84
        }
85
86 1
        return $saved;
87
    }
88
89
    /**
90
     * @return bool
91
     */
92 1
    protected function isEmpty(): bool
93
    {
94 1
        return $this->filesystem->has($this->path)
95 1
            && $this->filesystem->getSize($this->path) === 0;
96
    }
97
98
    /**
99
     * Checks if file is already in $path.
100
     * @return bool
101
     * @throws \League\Flysystem\FileNotFoundException
102
     */
103 1
    protected function isSaved(): bool
104
    {
105 1
        return $this->filesystem->has($this->path)
106 1
            && $this->filesystem->getTimestamp($this->path) > $this->file->getUpdatedAt();
107
    }
108
109
    /**
110
     * Saves file into $filesystem
111
     * @param $content
112
     * @return bool
113
     * @throws \InvalidArgumentException
114
     */
115 1
    protected function write($content): bool
116
    {
117 1
        if ($content === false || $content === null) {
118
            return false;
119
        }
120
121 1
        return \is_resource($content)
122
            ? $this->filesystem->putStream($this->path, $content)
123 1
            : $this->filesystem->put($this->path, $content);
124
    }
125
}