Factory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dafiti\Silex\Response;
4
5
use Dafiti\Silex\Response\Factory\Factorable;
6
use Dafiti\Silex\Response\Factory\Json;
7
use Symfony\Component\HttpFoundation;
8
9
class Factory implements Factorable
10
{
11
    /**
12
     * @var Factorable
13
     */
14
    private $factory;
15
16
    /**
17
     * @var array
18
     */
19
    private static $factoriesMap = [
20
        Json::CONTENT_TYPE => 'Json',
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    private static $factoriesNamespace = '\Dafiti\Silex\Response\Factory\\';
27
28
    /**
29
     * @param string $contentType
30
     */
31
    public function __construct($contentType)
32
    {
33
        $this->initFactory($contentType);
34
    }
35
36
    /**
37
     * @param string $contentType
38
     */
39
    private function initFactory($contentType)
40
    {
41
        foreach (self::$factoriesMap as $type => $factoryName) {
42
            $matched = is_int(strpos($contentType, $type));
43
            if ($matched) {
44
                $class = self::$factoriesNamespace.$factoryName;
45
                $this->factory = new $class($contentType);
46
            }
47
        }
48
    }
49
50
    /**
51
     * @param \Dafiti\Silex\Response $controllerResponse
52
     *
53
     * @return HttpFoundation\Response
54
     */
55
    public function create(\Dafiti\Silex\Response $controllerResponse)
56
    {
57
        $response = $this->factory->create($controllerResponse);
58
59
        return $response;
60
    }
61
}
62