Type   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
ccs 14
cts 14
cp 1
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A unserialize() 0 4 1
A serialize() 0 4 1
A getType() 0 4 1
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Contract\ObjectTypesInterface;
41
use Apparat\Object\Domain\Contract\SerializablePropertyInterface;
42
use Apparat\Object\Domain\Contract\TypeServiceInterface;
43
44
/**
45
 * Object type
46
 *
47
 * @package Apparat\Object
48
 * @subpackage Apparat\Object\Domain
49
 */
50
class Type implements SerializablePropertyInterface, ObjectTypesInterface
51
{
52
    /**
53
     * Object type
54
     *
55
     * @var string
56
     */
57
    protected $type = null;
58
    /**
59
     * Type service
60
     *
61
     * @var TypeServiceInterface
62
     */
63
    protected $typeService;
64
65
    /**
66
     * Type constructor
67
     *
68
     * @param string $type Object type
69
     * @param TypeServiceInterface $typeService Type service
70
     * @throws InvalidArgumentException If the type is not supported
71
     */
72 81
    public function __construct($type, TypeServiceInterface $typeService)
73
    {
74 81
        $this->typeService = $typeService;
75
76
        // If the type is not supported
77 81
        if (!$this->typeService->supportsType($type)) {
78 2
            throw new InvalidArgumentException(
79 2
                sprintf('Invalid object type "%s"', $type),
80 2
                InvalidArgumentException::INVALID_OBJECT_TYPE
81
            );
82
        }
83
84 80
        $this->type = $type;
85 80
    }
86
87
    /**
88
     * Unserialize the string representation of this property
89
     *
90
     * @param string $str Serialized property
91
     * @return Type Type property
92
     */
93 47
    public static function unserialize($str)
94
    {
95 47
        return Kernel::create(static::class, [$str]);
96
    }
97
98
    /**
99
     * Serialize the property
100
     *
101
     * @return mixed Property serialization
102
     */
103 2
    public function serialize()
104
    {
105 2
        return $this->getType();
106
    }
107
108
    /**
109
     * Return the object type
110
     *
111
     * @return string Object type
112
     */
113 56
    public function getType()
114
    {
115 56
        return $this->type;
116
    }
117
}
118