Passed
Pull Request — master (#190)
by Arman
02:47
created

FileSystemFactory::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Storage\Factories;
16
17
use Quantum\Libraries\Storage\Adapters\GoogleDrive\GoogleDriveFileSystemAdapter;
18
use Quantum\Libraries\Storage\Adapters\Dropbox\DropboxFileSystemAdapter;
19
use Quantum\Libraries\Storage\Adapters\Local\LocalFileSystemAdapter;
20
use Quantum\Libraries\Storage\Exceptions\FileSystemException;
21
use Quantum\Libraries\Storage\Contracts\CloudAppInterface;
22
use Quantum\Libraries\Storage\FileSystem;
23
use Quantum\Exceptions\BaseException;
24
25
/**
26
 * Class FileSystemFactory
27
 * @package Quantum\Libraries\Storage
28
 */
29
class FileSystemFactory
30
{
31
32
    /**
33
     * Supported adapters
34
     */
35
    const ADAPTERS = [
36
        FileSystem::LOCAL => LocalFileSystemAdapter::class,
37
        FileSystem::DROPBOX => DropboxFileSystemAdapter::class,
38
        FileSystem::GDRIVE => GoogleDriveFileSystemAdapter::class,
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $instances = [];
45
46
    /**
47
     * @param string $type
48
     * @param CloudAppInterface|null $cloudApp
49
     * @return FileSystem
50
     * @throws BaseException
51
     */
52
    public static function get(string $type = FileSystem::LOCAL, ?CloudAppInterface $cloudApp = null): FileSystem
53
    {
54
        if (!isset(self::$instances[$type])) {
55
            self::$instances[$type] = self::createInstance($type, $cloudApp);
56
        }
57
58
        return self::$instances[$type];
59
    }
60
61
    /**
62
     * @param string $type
63
     * @param CloudAppInterface|null $cloudApp
64
     * @return FileSystem
65
     * @throws BaseException
66
     */
67
    private static function createInstance(string $type, ?CloudAppInterface $cloudApp = null): FileSystem
68
    {
69
        if (!isset(self::ADAPTERS[$type])) {
70
            throw FileSystemException::adapterNotSupported($type);
71
        }
72
73
        $adapterClass = self::ADAPTERS[$type];
74
75
        return new FileSystem(new $adapterClass($cloudApp));
76
    }
77
}