Completed
Push — master ( 63cdb6...e2968d )
by Bas
06:46 queued 04:49
created

SftpAdapterFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 43.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 82.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 18
loc 41
ccs 14
cts 17
cp 0.8235
rs 10
c 1
b 0
f 0

2 Methods

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

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
namespace BsbFlysystem\Adapter\Factory;
4
5
use BsbFlysystem\Exception\RequirementsException;
6
use BsbFlysystem\Exception\UnexpectedValueException;
7
use League\Flysystem\Sftp\SftpAdapter as Adapter;
8
use Zend\ServiceManager\FactoryInterface;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
class SftpAdapterFactory extends AbstractAdapterFactory
12
{
13
    /**
14
     * @inheritdoc
15
     */
16 1
    public function doCreateService(ServiceLocatorInterface $serviceLocator)
17
    {
18 1
        if (!class_exists(\League\Flysystem\Sftp\SftpAdapter::class)) {
19
            throw new RequirementsException(
20
                ['league/flysystem-sftp'],
21
                'Sftp'
22
            );
23
        }
24
25 1
        $adapter = new Adapter($this->options);
26
27 1
        return $adapter;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 7 View Code Duplication
    protected function validateConfig()
34
    {
35 7
        if (!isset($this->options['host'])) {
36 1
            throw new UnexpectedValueException("Missing 'host' as option");
37
        }
38
39 6
        if (!isset($this->options['port'])) {
40 1
            throw new UnexpectedValueException("Missing 'port' as option");
41
        }
42
43 5
        if (!isset($this->options['username'])) {
44 1
            throw new UnexpectedValueException("Missing 'username' as option");
45
        }
46
47 4
        if (!isset($this->options['password']) && !isset($this->options['privateKey'])) {
48 1
            throw new UnexpectedValueException("Missing either 'password' or 'privateKey' as option");
49
        }
50 3
    }
51
}
52