Passed
Pull Request — master (#190)
by Arman
04:05
created

FileSystemFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 9 2
A get() 0 7 2
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
}