Test Failed
Push — master ( 6afeb6...f4c35a )
by
unknown
04:09 queued 02:18
created

AdapterPool::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Silk\Database;
4
5
use Zend\Db\Adapter\Adapter;
6
use Zend\Db\TableGateway\TableGateway;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Silk\Database\TableGateway.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
8
9
/**
10
 * AdapterPool
11
 *
12
 * @author  Lucas A. de Araújo <[email protected]>
13
 * @package Silk\Database
14
 */
15
class AdapterPool
16
{
17
    /**
18
     * Pool of adapters
19
     *
20
     * @var array
21
     */
22
    protected static $pool = [];
23
24
    /**
25
     * Construtor
26
     */
27
    public function __construct()
28
    {
29
        // Conexão com o MySQL
30
        $this->add('Default', GlobalAdapterFeature::getStaticAdapter());
31
    }
32
    
33
    /**
34
     * @param $key
35
     * @param Adapter $adapter
36
     * @return $this
37
     */
38
    public function add($key, Adapter $adapter)
39
    {
40
        self::$pool[$key] = $adapter;
41
        return $this;
42
    }
43
44
    /**
45
     * @param $key
46
     * @return $this
47
     */
48
    public function remove($key)
49
    {
50
        unset(self::$pool[$key]);
51
        return $this;
52
    }
53
54
    /**
55
     * @param $key
56
     * @return Adapter
57
     */
58
    public function get($key)
59
    {
60
        return self::$pool[$key];
61
    }
62
}
63