Passed
Push — develop ( c3a5bd...0fb661 )
by Jens
17:20
created

Resource::fieldDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Common;
7
8
use DateTime;
9
10
/**
11
 * @package Commercetools\Core\Model\Common
12
 * @method getId()
13
 */
14
abstract class Resource extends JsonObject implements ReferenceObjectInterface
15
{
16 1
    public function fieldDefinitions()
17
    {
18
        return [
19 1
            'id' => [static::TYPE => 'string'],
20 1
            'version' => [static::TYPE => 'int'],
21
            'createdAt' => [
22 1
                static::TYPE => DateTime::class,
23 1
                static::DECORATOR => DateTimeDecorator::class
24
            ],
25
            'lastModifiedAt' => [
26 1
                static::TYPE => DateTime::class,
27 1
                static::DECORATOR => DateTimeDecorator::class
28
            ],
29
        ];
30
    }
31
32 209
    public function getReferenceIdentifier()
33
    {
34 209
        return $this->getId();
35
    }
36
37
    /**
38
     * @return Reference
39
     */
40 209
    public function getReference()
41
    {
42 209
        $className = trim(get_called_class(), '\\');
43 209
        $referenceClass = $className . 'Reference';
44 209
        if (class_exists($referenceClass)) {
45 208
            $reference = call_user_func_array(
46 208
                $referenceClass . '::ofId',
47 208
                [$this->getReferenceIdentifier(), $this->getContextCallback()]
48
            );
49
        } else {
50 1
            $classParts = explode('\\', $className);
51 1
            $class = lcfirst(array_pop($classParts));
52 1
            $type = strtolower(preg_replace('/([A-Z])/', '-$1', $class));
53
54 1
            $reference = Reference::ofTypeAndId($type, $this->getReferenceIdentifier(), $this->getContextCallback());
55
        }
56 209
        $reference->setObj($this);
57
58 209
        return $reference;
59
    }
60
}
61