Passed
Push — master ( dc4af7...7bb9d6 )
by alpha
15:00
created

AppendFile::macro()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
nc 1
nop 0
dl 0
loc 18
rs 9.9
c 1
b 0
f 1
1
<?php
2
3
namespace AlphaSnow\LaravelFilesystem\Aliyun\Macros;
4
5
use AlphaSnow\Flysystem\Aliyun\AliyunAdapter;
6
use AlphaSnow\Flysystem\Aliyun\AliyunException;
7
use Illuminate\Filesystem\FilesystemAdapter;
8
use League\Flysystem\Config;
9
use OSS\Core\OssException;
10
use Closure;
11
12
class AppendFile implements AliyunMacro
13
{
14
    public function name(): string
15
    {
16
        return "appendFile";
17
    }
18
19
    public function macro(): Closure
20
    {
21
        return function (string $path, string $content, int $position = 0, array $options = []) {
22
            try {
23
                /**
24
                 * @var FilesystemAdapter $this
25
                 * @var AliyunAdapter $adapter
26
                 */
27
                $adapter = $this->getAdapter();
28
                return $adapter->getClient()->appendFile(
29
                    $adapter->getBucket(),
30
                    $adapter->getPrefixer()->prefixPath($path),
31
                    $content,
32
                    $position,
33
                    $adapter->getOptions()->mergeConfig(new Config($options))
34
                );
35
            } catch (OssException $exception) {
36
                throw new AliyunException($exception->getErrorMessage(), 0, $exception);
37
            }
38
        };
39
    }
40
}
41