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

Resource   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 47
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferenceIdentifier() 0 4 1
A getReference() 0 20 2
A fieldDefinitions() 0 15 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