FormatterFactory::setup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Glooby\Debug\Formatter;
4
5
use GuzzleHttp\Exception\TransferException;
6
use GuzzleHttp\Message\RequestInterface;
7
use GuzzleHttp\Message\ResponseInterface;
8
9
/**
10
 * @author Emil Kilhage
11
 */
12
class FormatterFactory implements FormatterFactoryInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private static $defaultFormatter = PrintrFormatter::class;
18
19
    /**
20
     * @param string $defaultFormatter
21
     */
22
    public static function setDefaultFormatter($defaultFormatter)
23
    {
24
        self::$defaultFormatter = $defaultFormatter;
25
    }
26
27
    /**
28
     * @var array
29
     */
30
    private static $classMap = [
31
        \SimpleXMLElement::class => SimpleXMLElementFormatter::class,
32
        TransferException::class => Guzzle\GuzzleExceptionFormatter::class,
33
        \GuzzleHttp\Stream\Stream::class => StringFactoryFormatter::class,
34
        ResponseInterface::class => Guzzle\ResponseFormatter::class,
35
        RequestInterface::class => Guzzle\RequestFormatter::class,
36
        \Exception::class => ExceptionFormatter::class,
37
    ];
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function factory($output)
43
    {
44
        if (is_string($output) || is_int($output)) {
45
            return $this->setup(StringFactoryFormatter::class);
46
        } elseif (is_object($output)) {
47
            foreach (self::$classMap as $className => $formatterClassName) {
48
                if (is_a($output, $className)) {
49
                    return $this->setup($formatterClassName);
50
                }
51
            }
52
        }
53
54
        return $this->setup(self::$defaultFormatter);
55
    }
56
57
    /**
58
     * @param string $className
59
     * @return FormatterInterface
60
     */
61
    private function setup($className)
62
    {
63
        $formatter = new $className();
64
        return $formatter;
65
    }
66
}
67