Factory::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 8
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Ftp;
3
4 View Code Duplication
class Factory
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
5
{
6
    const DRIVER_FTP = 'Ftp';
7
    const DRIVER_SFTP = 'Sftp';
8
    const DRIVER_FTP_IMPLICIT_SSL = 'FtpImplicitSSL';
9
10
    private static $instance;
11
    
12 1
    private function __construct()
13
    {
14 1
    }
15
16
    /**
17
     * Design pattern Singleton
18
     * @codeCoverageIgnore
19
     */
20
    private function __clone()
21
    {
22
    }
23
24
    /**
25
     * @return self
26
     */
27 4
    final public static function getInstance()
28
    {
29 4
        if (!self::$instance) {
30 1
            $class = get_called_class();
31 1
            self::$instance = new $class;
32
        }
33 4
        return self::$instance;
34
    }
35
36
    /**
37
     * @param string $driver
38
     * @param array $params
39
     * @return FtpInterface
40
     * @throws Exception
41
     */
42 4
    final public function create($driver = self::DRIVER_FTP, $params = array())
43
    {
44 4
        $class = $this->getClassNameForDriver($driver);
45 4
        if (!class_exists($class)) {
46 1
            throw new Exception('Unable to create ' . $class);
47
        }
48 3
        $instance = new $class($params);
49 3
        if (!$instance instanceof FtpInterface) {
50 1
            throw new Exception('Unable to create ' . $class);
51
        }
52 2
        return $instance;
53
    }
54
55
    /**
56
     * Get full class name to create
57
     * @param string $driver
58
     * @return string
59
     */
60 2
    protected function getClassNameForDriver($driver)
61
    {
62 2
        return __NAMESPACE__ . '\Driver\\' . $driver;
63
    }
64
}
65