Completed
Push — 6.0 ( c6d21a...a43f83 )
by yun
15:10 queued 11:06
created

Driver::putFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\filesystem;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\Cached\CachedAdapter;
7
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
8
use League\Flysystem\Filesystem;
9
use think\App;
10
use think\File;
11
12
/**
13
 * Class Driver
14
 * @package think\filesystem
0 ignored issues
show
Coding Style introduced by
Package name "think\filesystem" is not valid; consider "Thinkfilesystem" instead
Loading history...
15
 * @mixin Filesystem
1 ignored issue
show
Coding Style introduced by
Tag value for @mixin tag indented incorrectly; expected 3 spaces but found 1
Loading history...
16
 */
4 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
17
abstract class Driver
18
{
19
20
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
21
    protected $app;
22
23
    /** @var Filesystem */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
24
    protected $filesystem;
25
26
    protected $config = [];
27
28 3
    public function __construct(App $app, $config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
29
    {
30 3
        $this->app    = $app;
31 3
        $this->config = array_merge($this->config, $config);
32
33 3
        $adapter          = $this->createAdapter();
34 3
        $this->filesystem = $this->createFilesystem($adapter);
35 3
    }
36
37 2
    protected function createCacheStore($config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createCacheStore()
Loading history...
38
    {
39 2
        if ($config === true) {
40 2
            return new MemoryStore;
41
        }
42
43 1
        return new CacheStore(
44 1
            $this->app->cache->store($config['store']),
45 1
            $config['prefix'] ?? 'flysystem',
46 1
            $config['expire'] ?? null
47
        );
48
    }
49
50
    abstract protected function createAdapter(): AdapterInterface;
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createAdapter()
Loading history...
51
52 3
    protected function createFilesystem(AdapterInterface $adapter)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createFilesystem()
Loading history...
53
    {
54 3
        if (!empty($this->config['cache'])) {
55 2
            $adapter = new CachedAdapter($adapter, $this->createCacheStore($this->config['cache']));
56
        }
57
58 3
        $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url']));
59
60 3
        return new Filesystem($adapter, count($config) > 0 ? $config : null);
61
    }
62
63
    /**
64
     * 保存文件
65
     * @param string               $path
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
66
     * @param File                 $file
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
67
     * @param null|string|\Closure $rule
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
68
     * @param array                $options
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
69
     * @return bool|string
70
     */
71 1
    public function putFile($path, $file, $rule = null, $options = [])
72
    {
73 1
        return $this->putFileAs($path, $file, $file->hashName($rule), $options);
74
    }
75
76
    /**
77
     * 指定文件名保存文件
78
     * @param string $path
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
79
     * @param File   $file
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
80
     * @param string $name
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
81
     * @param array  $options
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
82
     * @return bool|string
83
     */
84 1
    public function putFileAs($path, $file, $name, $options = [])
85
    {
86 1
        $stream = fopen($file->getRealPath(), 'r');
87
88 1
        $result = $this->putStream(
0 ignored issues
show
Bug introduced by
The method putStream() does not exist on think\filesystem\Driver. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        /** @scrutinizer ignore-call */ 
89
        $result = $this->putStream(
Loading history...
89 1
            $path = trim($path . '/' . $name, '/'), $stream, $options
90
        );
91
92 1
        if (is_resource($stream)) {
93 1
            fclose($stream);
94
        }
95
96 1
        return $result ? $path : false;
97
    }
98
99 2
    public function __call($method, $parameters)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
100
    {
101 2
        return $this->filesystem->$method(...$parameters);
102
    }
103
}
104