Completed
Push — 7.4 ( 310a44...3b2076 )
by Nikolaos
05:19
created

SerializerFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 42
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newInstance() 0 6 1
A getAdapters() 0 11 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Storage;
15
16
use Phalcon\Factory\AbstractFactory;
17
use Phalcon\Factory\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Phalcon\Storage\Exception.

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...
18
use Phalcon\Factory\FactoryTrait;
19
use Phalcon\Storage\Serializer\SerializerInterface;
20
21
/**
22
 * Class SerializerFactory
23
 *
24
 * @package Phalcon\Storage
25
 */
26
class SerializerFactory
27
{
28
    use FactoryTrait;
29
30
    /**
31
     * SerializerFactory constructor.
32
     *
33
     * @param array $services
34
     */
35 239
    public function __construct(array $services = [])
36
    {
37 239
        $this->init($services);
38 239
    }
39
40
    /**
41
     * @param string $name
42
     *
43
     * @return SerializerInterface
44
     * @throws Exception
45
     */
46 145
    public function newInstance(string $name): SerializerInterface
47
    {
48 145
        $definition = $this->getService($name);
49
50 144
        return new $definition();
51
    }
52
53
    /**
54
     * @return array
55
     */
56 239
    protected function getAdapters(): array
57
    {
58
        return [
59 239
            "base64"   => 'Phalcon\Storage\Serializer\Base64',
60
            "igbinary" => 'Phalcon\Storage\Serializer\Igbinary',
61
            "json"     => 'Phalcon\Storage\Serializer\Json',
62
            "msgpack"  => 'Phalcon\Storage\Serializer\Msgpack',
63
            "none"     => 'Phalcon\Storage\Serializer\None',
64
            "php"      => 'Phalcon\Storage\Serializer\Php',
65
        ];
66
    }
67
}
68