Passed
Push — 6.0 ( 3d2548...825cd5 )
by yun
04:38
created

Driver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
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\Adapter\AbstractAdapter;
6
use League\Flysystem\AdapterInterface;
7
use League\Flysystem\Cached\CachedAdapter;
8
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
9
use League\Flysystem\Filesystem;
10
use think\App;
11
use think\File;
12
13
/**
14
 * Class Driver
15
 * @package think\filesystem
0 ignored issues
show
Coding Style introduced by
Package name "think\filesystem" is not valid; consider "Thinkfilesystem" instead
Loading history...
16
 * @mixin Filesystem
17
 */
18
abstract class Driver
19
{
20
21
    /** @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...
22
    protected $app;
23
24
    /** @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...
25
    protected $filesystem;
26
27
    protected $config = [];
28
29 3
    public function __construct(App $app, $config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
30
    {
31 3
        $this->app    = $app;
32 3
        $this->config = array_merge($this->config, $config);
33
34 3
        $adapter          = $this->createAdapter();
35 3
        $this->filesystem = $this->createFilesystem($adapter);
36 3
    }
37
38 2
    protected function createCacheStore($config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createCacheStore()
Loading history...
39
    {
40 2
        if ($config === true) {
41 2
            return new MemoryStore;
42
        }
43
44 1
        return new CacheStore(
45 1
            $this->app->cache->store($config['store']),
46 1
            $config['prefix'] ?? 'flysystem',
47 1
            $config['expire'] ?? null
48
        );
49
    }
50
51
    abstract protected function createAdapter(): AdapterInterface;
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createAdapter()
Loading history...
52
53 3
    protected function createFilesystem(AdapterInterface $adapter)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createFilesystem()
Loading history...
54
    {
55 3
        if (!empty($this->config['cache'])) {
56 2
            $adapter = new CachedAdapter($adapter, $this->createCacheStore($this->config['cache']));
57
        }
58
59 3
        $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url']));
60
61 3
        return new Filesystem($adapter, count($config) > 0 ? $config : null);
62
    }
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $path should have a doc-comment as per coding-style.
Loading history...
65
     * 获取文件完整路径
66
     * @param $path
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
67
     * @return string
68
     */
69
    public function path($path)
70
    {
71
        $adapter = $this->filesystem->getAdapter();
72
        if ($adapter instanceof AbstractAdapter) {
73
            return $adapter->applyPathPrefix($path);
74
        }
75
        return $path;
76
    }
77
78
    /**
79
     * 保存文件
80
     * @param string               $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @param File                 $file
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @param null|string|\Closure $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     * @param array                $options
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @return bool|string
85
     */
86 1
    public function putFile($path, $file, $rule = null, $options = [])
87
    {
88 1
        return $this->putFileAs($path, $file, $file->hashName($rule), $options);
89
    }
90
91
    /**
92
     * 指定文件名保存文件
93
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @param File   $file
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
95
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
96
     * @param array  $options
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
97
     * @return bool|string
98
     */
99 1
    public function putFileAs($path, $file, $name, $options = [])
100
    {
101 1
        $stream = fopen($file->getRealPath(), 'r');
102
103 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

103
        /** @scrutinizer ignore-call */ 
104
        $result = $this->putStream(
Loading history...
104 1
            $path = trim($path . '/' . $name, '/'), $stream, $options
105
        );
106
107 1
        if (is_resource($stream)) {
108 1
            fclose($stream);
109
        }
110
111 1
        return $result ? $path : false;
112
    }
113
114 2
    public function __call($method, $parameters)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
115
    {
116 2
        return $this->filesystem->$method(...$parameters);
117
    }
118
}
119