Completed
Push — master ( 2d2f76...c06362 )
by Sebastian
02:09
created

PhpObjectContent   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A asString() 0 4 1
A getContentType() 0 4 1
A ensureIsObject() 0 6 2
1
<?php
2
namespace Kartenmacherei\RestFramework\Response\Content;
3
4
use Kartenmacherei\RestFramework\EnsureException;
5
6
class PhpObjectContent implements Content
7
{
8
    /**
9
     * @var mixed object
10
     */
11
    private $object;
12
13
    /**
14
     * @param mixed $object
15
     */
16
    public function __construct($object)
17
    {
18
        $this->ensureIsObject($object);
19
        $this->object = $object;
20
    }
21
22
23
    /**
24
     * @return string
25
     */
26
    public function asString(): string
27
    {
28
        return serialize($this->object);
29
    }
30
31
    /**
32
     * @return ContentType
33
     */
34
    public function getContentType(): ContentType
35
    {
36
        return new PlainContentType();
37
    }
38
39
    /**
40
     * @param $value
41
     * @throws EnsureException
42
     */
43
    private function ensureIsObject($value)
44
    {
45
        if (!is_object($value)) {
46
            throw new EnsureException('Value must be an object');
47
        }
48
    }
49
}
50