Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

FlySystemFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 3
rs 9.568
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Parser;
17
18
use Flyfinder\Finder;
19
use League\Flysystem\Adapter\Local;
20
use League\Flysystem\Filesystem;
21
use League\Flysystem\FilesystemInterface;
22
use League\Flysystem\MountManager;
23
use LogicException;
24
use phpDocumentor\Dsn;
25
use phpDocumentor\Parser\FileSystemFactory;
26
27
final class FlySystemFactory implements FileSystemFactory
28
{
29
    /** @var MountManager */
30
    private $mountManager;
31
32 4
    public function __construct(MountManager $mountManager)
33
    {
34 4
        $this->mountManager = $mountManager;
35 4
    }
36
37
    /**
38
     * Returns a Filesystem instance based on the scheme of the provided Dsn
39
     */
40 4
    public function create(Dsn $dsn): FilesystemInterface
41
    {
42 4
        $dsnId = hash('md5', (string) $dsn);
43
44
        try {
45 4
            $filesystem = $this->mountManager->getFilesystem($dsnId);
46 3
        } catch (LogicException $e) {
47 3
            if ($dsn->getScheme() === 'file') {
48 2
                $path = $dsn->getPath();
49 2
                $filesystem = new Filesystem(new Local($path, LOCK_EX, Local::SKIP_LINKS));
50
            } else {
51
                //This will be implemented as soon as the CloneRemoteGitToLocal adapter is finished
52 1
                throw new \InvalidArgumentException('http and https are not supported yet');
53
            }
54
55 2
            $this->mountManager->mountFilesystem($dsnId, $filesystem);
56
        }
57
58 3
        $filesystem->addPlugin(new Finder());
59
60 3
        return $filesystem;
61
    }
62
}
63