Passed
Push — develop ( 05cd66...991049 )
by Jens
09:34 queued 14s
created

Resource::getReferenceIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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
    public function fieldDefinitions()
17
    {
18
        return [
19
            'id' => [static::TYPE => 'string'],
20
            'version' => [static::TYPE => 'int'],
21
            'createdAt' => [
22
                static::TYPE => DateTime::class,
23
                static::DECORATOR => DateTimeDecorator::class
24
            ],
25
            'lastModifiedAt' => [
26
                static::TYPE => DateTime::class,
27
                static::DECORATOR => DateTimeDecorator::class
28
            ],
29
        ];
30
    }
31
32 335
    public function getReferenceIdentifier()
33
    {
34 335
        return $this->getId();
35
    }
36
37
    /**
38
     * @return Reference
39
     */
40 335
    public function getReference()
41
    {
42 335
        $className = trim(get_called_class(), '\\');
43 335
        $referenceClass = $className . 'Reference';
44 335
        if (class_exists($referenceClass)) {
45 334
            $reference = call_user_func_array(
46 334
                $referenceClass . '::ofId',
47 334
                [$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 335
        $reference->setObj($this);
57
58 335
        return $reference;
59
    }
60
}
61