Completed
Pull Request — master (#42)
by Florent
12:58
created

SftpAdapterFactory::doCreateService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BsbFlysystem\Adapter\Factory;
6
7
use BsbFlysystem\Exception\RequirementsException;
8
use BsbFlysystem\Exception\UnexpectedValueException;
9
use League\Flysystem\AdapterInterface;
10
use League\Flysystem\Sftp\SftpAdapter as Adapter;
11
use Psr\Container\ContainerInterface;
12
13
class SftpAdapterFactory extends AbstractAdapterFactory
14
{
15
    public function doCreateService(ContainerInterface $container): AdapterInterface
16
    {
17
        if (! class_exists(\League\Flysystem\Sftp\SftpAdapter::class)) {
18
            throw new RequirementsException(
19
                ['league/flysystem-sftp'],
20
                'Sftp'
21
            );
22
        }
23
24
        $adapter = new Adapter($this->options);
25
26
        return $adapter;
27
    }
28
29 6 View Code Duplication
    protected function validateConfig()
30
    {
31 6
        if (! isset($this->options['host'])) {
32 1
            throw new UnexpectedValueException("Missing 'host' as option");
33
        }
34
35 5
        if (! isset($this->options['port'])) {
36 1
            throw new UnexpectedValueException("Missing 'port' as option");
37
        }
38
39 4
        if (! isset($this->options['username'])) {
40 1
            throw new UnexpectedValueException("Missing 'username' as option");
41
        }
42
43 3
        if (! isset($this->options['password']) && ! isset($this->options['privateKey'])) {
44 1
            throw new UnexpectedValueException("Missing either 'password' or 'privateKey' as option");
45
        }
46 2
    }
47
}
48