Completed
Push — issue/v4/1096-manage-applicati... ( 235971...dce370 )
by Paolo
03:22
created

FilesystemAdapter::getInnerAdapter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2017 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace BEdita\Core\Filesystem;
15
16
use Cake\Core\InstanceConfigTrait;
17
use League\Flysystem\AdapterInterface;
18
19
/**
20
 * Filesystem adapter.
21
 *
22
 * This abstract class is a wrapper around Flysystem adapters, to ensure they can be instantiated
23
 * in a consistent way by delegating adapter's specific initialization logic to subclasses.
24
 *
25
 * Also, subclasses of this class are responsible for building public URLs from which files can
26
 * be accessed, although a generic implementation is present in this abstract class.
27
 *
28
 * @since 4.0.0
29
 */
30
abstract class FilesystemAdapter
31
{
32
33
    use InstanceConfigTrait;
34
35
    /**
36
     * Default configuration for adapter.
37
     *
38
     * @var array
39
     */
40
    protected $_defaultConfig = [
41
        'baseUrl' => null,
42
        'visibility' => 'public',
43
    ];
44
45
    /**
46
     * Inner adapter instance.
47
     *
48
     * @var \League\Flysystem\AdapterInterface
49
     */
50
    protected $adapter;
51
52
    /**
53
     * Initialize filesystem adapter class.
54
     *
55
     * @param array $config Configuration.
56
     * @return bool
57
     */
58
    public function initialize(array $config)
59
    {
60
        $this->setConfig($config);
61
62
        return true;
63
    }
64
65
    /**
66
     * Get the inner adapter class.
67
     *
68
     * @return \League\Flysystem\AdapterInterface
69
     */
70
    public function getInnerAdapter()
71
    {
72
        if (!empty($this->adapter)) {
73
            return $this->adapter;
74
        }
75
76
        $adapter = $this->buildAdapter($this->getConfig());
77
        if (!($adapter instanceof AdapterInterface)) {
0 ignored issues
show
Bug introduced by
The class League\Flysystem\AdapterInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
78
            throw new \RuntimeException(
79
                sprintf('Filesystem adapters must use %s as a base class.', AdapterInterface::class)
80
            );
81
        }
82
83
        return $this->adapter = $adapter;
84
    }
85
86
    /**
87
     * Build the inner adapter from configuration.
88
     *
89
     * @param array $config Adapter configuration.
90
     * @return \League\Flysystem\AdapterInterface
91
     */
92
    abstract protected function buildAdapter(array $config);
93
94
    /**
95
     * Get public URL for an item.
96
     *
97
     * @param string $path Resource path.
98
     * @return string
99
     */
100
    public function getPublicUrl($path)
101
    {
102
        return sprintf(
103
            '%s/%s',
104
            rtrim($this->getConfig('baseUrl'), '/'),
105
            ltrim($path, '/')
106
        );
107
    }
108
109
    /**
110
     * Get default visibility for this adapter.
111
     *
112
     * @return string|null
113
     */
114
    public function getVisibility()
115
    {
116
        return $this->getConfig('visibility');
117
    }
118
}
119