Completed
Push — develop ( 991012...b60d88 )
by Jens
08:17
created

Message::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185
Metric Value
dl 8
loc 8
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Message;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
use Commercetools\Core\Model\Common\Reference;
11
use Commercetools\Core\Model\Common\DateTimeDecorator;
12
13
/**
14
 * @package Commercetools\Core\Model\Message
15
 * @link https://dev.commercetools.com/http-api-projects-messages.html#message
16
 * @method string getId()
17
 * @method Message setId(string $id = null)
18
 * @method DateTimeDecorator getCreatedAt()
19
 * @method Message setCreatedAt(\DateTime $createdAt = null)
20
 * @method int getSequenceNumber()
21
 * @method Message setSequenceNumber(int $sequenceNumber = null)
22
 * @method Reference getResource()
23
 * @method Message setResource(Reference $resource = null)
24
 * @method int getResourceVersion()
25
 * @method Message setResourceVersion(int $resourceVersion = null)
26
 * @method string getType()
27
 * @method Message setType(string $type = null)
28
 */
29
class Message extends JsonObject
30
{
31
    const MESSAGE_TYPE = '';
32
33
    /**
34
     * @param array $data
35
     * @param Context|callable $context
36
     */
37 3 View Code Duplication
    public function __construct(array $data = [], $context = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 3
        parent::__construct($data, $context);
40 3
        $type = static::MESSAGE_TYPE;
41 3
        if (!empty($type)) {
42
            $this->setType(static::MESSAGE_TYPE);
43
        }
44 3
    }
45
46 2
    public function fieldDefinitions()
47
    {
48
        return [
49 2
            'id' => [static::TYPE => 'string'],
50
            'createdAt' => [
51 2
                static::TYPE => '\DateTime',
52 2
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
53
            ],
54 2
            'sequenceNumber' => [static::TYPE => 'int'],
55 2
            'resource' => [static::TYPE => '\Commercetools\Core\Model\Common\Reference'],
56 2
            'resourceVersion' => [static::TYPE => 'int'],
57 2
            'type' => [static::TYPE => 'string'],
58
        ];
59
    }
60
61
    /**
62
     * @param array $data
63
     * @param Context|callable $context
64
     * @return static
65
     */
66 1
    public static function fromArray(array $data, $context = null)
67
    {
68 1
        if (isset($data['type'])) {
69
            $className = '\Commercetools\Core\Model\Message\\' . ucfirst($data['type']) . 'Message';
70
            if (class_exists($className)) {
71
                return new $className($data, $context);
72
            }
73
        }
74 1
        return new static($data, $context);
75
    }
76
}
77