Passed
Pull Request — master (#35)
by
unknown
13:31
created

src/File/Store/AnyCloud.php (1 issue)

Severity
1
<?php
2
3
namespace AnyCloud\File\Store;
4
5
use League\Flysystem\Filesystem;
6
use League\Flysystem\FilesystemException;
7
use League\Flysystem\Visibility;
8
use Omeka\File\Exception\ConfigException;
9
use Omeka\File\Store\StoreInterface;
10
11
class AnyCloud implements StoreInterface
12
{
13
    private const AWS_BASED = ['aws', 'wasabi', 'digital_ocean', 'scaleway'];
14
    private $remoteFilesystem;
15
    private $configs;
16
    private $uri;
17
    private $adapter;
18
19
    /**
20
     * Initiate Any Cloud Module.
21
     *
22
     * @param Filesystem  $remoteFilesystem Filesystem to use
23
     * @param array       $configs          Array containing `config` data
24
     * @param string|null $uri              URI structure for file
25
     * @param object      $adapter          Adapter that can be used to create temporary links or call other methods of
26
     *                                      the Flysystem Adapter
27
     */
28
    public function __construct(Filesystem $remoteFilesystem, $configs, $uri = null, $adapter = null)
29
    {
30
        $this->remoteFilesystem = $remoteFilesystem;
31
        $this->configs = $configs;
32
        $this->uri = $uri;
33
        $this->adapter = $adapter;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function put($source, $storagePath): void
40
    {
41
        try {
42
            $contents = fopen($source, 'r');
43
            $config = ['visibility' => Visibility::PUBLIC];
44
            $this->remoteFilesystem->writeStream($storagePath, $contents, $config);
45
            if (is_resource($contents)) {
46
                fclose($contents);
47
            }
48
        } catch (ConfigException $e) {
49
            echo 'Any Cloud Error: '.$e->getMessage()."\n";
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function delete($storagePath): void
57
    {
58
        try {
59
            $this->remoteFilesystem->delete($storagePath);
60
        } catch (FilesystemException $e) {
61
            echo 'Any Cloud Error: '.$e->getMessage()."\n";
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getUri($storagePath): string
69
    {
70
        if (method_exists($this->adapter, 'getUrl')) {
71
            // Kludgy solution to working with temporary Dropbox URIs
72
            return $this->adapter->getUrl($storagePath);
73
        } else {
74
            return $this->remoteFilesystem->publicUrl($storagePath);
75
        }
76
        // Normal method for grabbing URIs from `AnyCloudFactory` (for everything except Dropbox)
77
        try {
0 ignored issues
show
TryCatchNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
78
            return $this->uri.'/'.$storagePath;
79
        } catch (ConfigException $e) {
80
            echo 'Any Cloud Error: '.$e->getMessage()."\n";
81
        }
82
    }
83
}
84