Completed
Push — master ( 4a3469...dbb7ba )
by Kanstantsin
03:17
created

Saver::save()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.246

Importance

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