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

SftpAdapterFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 51.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 18
loc 35
ccs 10
cts 17
cp 0.5881
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateConfig() 18 18 6
A doCreateService() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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