Passed
Push — master ( d1ccc3...a5920b )
by Nicolas
04:01
created

Standard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Puzzle\AMQP\Messages\BodyFactories;
4
5
use Psr\Log\LoggerAwareTrait;
6
use Psr\Log\NullLogger;
7
use Puzzle\AMQP\Messages\TypedBodyFactories;
8
use Puzzle\AMQP\Messages\BodyFactory;
9
use Puzzle\AMQP\Messages\ContentType;
10
use Puzzle\AMQP\Messages\Bodies\NullBody;
11
use Puzzle\AMQP\Messages\TypedBodyFactory;
12
13
class Standard implements BodyFactory
14
{
15
    use LoggerAwareTrait;
16
    
17
    private
18
        $factories;
19
    
20 33
    public function __construct()
21
    {
22 33
        $this->logger = new NullLogger();
23 33
        $this->initializeFactories();
24 33
    }
25
    
26 33
    private function initializeFactories()
27
    {
28 33
        $this->factories = [
29 33
            ContentType::TEXT => new TypedBodyFactories\Text(),
30 33
            ContentType::JSON => new TypedBodyFactories\Json(),
31 33
            ContentType::BINARY => new TypedBodyFactories\Binary(),
32
        ];
33 33
    }
34
    
35 2
    public function handleContentType($contentType, TypedBodyFactory $factory)
36
    {
37 2
        $this->factories[$contentType] = $factory;
38
        
39 2
        return $this;
40
    }
41
    
42 24
    public function build($contentType, $contentAsTransported)
43
    {
44 24
        if(isset($this->factories[$contentType]))
45 24
        {
46 19
            return $this->factories[$contentType]->build($contentAsTransported);
47
        }
48
        
49 6
        $this->logger->warning(__CLASS__ . ": unknown content-type, use empty body");
50
        
51 6
        return new NullBody();
52
    }
53
}
54