Completed
Push — master ( 65f512...fe0390 )
by Łukasz
26:26
created

ContentProxy   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 112
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 28
loc 112
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 16 4
A __isset() 12 12 4
A getContentInfo() 0 15 4
A getVersionInfo() 0 8 2
A getFieldValue() 8 8 2
A getFields() 0 8 2
A getFieldsByLanguage() 0 8 2
A getField() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository\Values\Content;
8
9
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
10
use eZ\Publish\Core\Repository\Values\GeneratorProxyTrait;
11
12
/**
13
 * This class represents a (lazy loaded) proxy for a content value.
14
 *
15
 * @internal Meant for internal use by Repository, type hint against API object instead.
16
 */
17
class ContentProxy extends APIContent
18
{
19
    use GeneratorProxyTrait;
20
21
    /**
22
     * @var \eZ\Publish\API\Repository\Values\Content\Content|null
23
     */
24
    protected $object;
25
26
    /**
27
     * @var ContentContentInfoProxy|null
28
     */
29
    protected $contentInfoProxy;
30
31
    public function __get($name)
32
    {
33
        if ($name === 'id') {
34
            return $this->id;
35
        }
36
37
        if ($name === 'contentInfo') {
38
            return $this->getContentInfo();
39
        }
40
41
        if ($this->object === null) {
42
            $this->loadObject();
43
        }
44
45
        return $this->object->$name;
46
    }
47
48 View Code Duplication
    public function __isset($name)
49
    {
50
        if ($name === 'id' || $name === 'contentInfo') {
51
            return true;
52
        }
53
54
        if ($this->object === null) {
55
            $this->loadObject();
56
        }
57
58
        return isset($this->object->$name);
59
    }
60
61
    /**
62
     * Return content info, in proxy form if we have not loaded object yet.
63
     *
64
     * For usage in among others DomainMapper->buildLocation() to make sure we can lazy load content info retrieval.
65
     *
66
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
67
     */
68
    protected function getContentInfo()
69
    {
70
        if ($this->object === null) {
71
            if ($this->contentInfoProxy === null) {
72
                $this->contentInfoProxy = new ContentContentInfoProxy($this, $this->id);
73
            }
74
75
            return $this->contentInfoProxy;
76
        } elseif ($this->contentInfoProxy !== null) {
77
            // Remove ref when we no longer need the proxy
78
            $this->contentInfoProxy = null;
79
        }
80
81
        return $this->object->getVersionInfo()->getContentInfo();
82
    }
83
84
    public function getVersionInfo()
85
    {
86
        if ($this->object === null) {
87
            $this->loadObject();
88
        }
89
90
        return $this->object->getVersionInfo();
91
    }
92
93 View Code Duplication
    public function getFieldValue($fieldDefIdentifier, $languageCode = null)
94
    {
95
        if ($this->object === null) {
96
            $this->loadObject();
97
        }
98
99
        return $this->object->getFieldValue($fieldDefIdentifier, $languageCode);
100
    }
101
102
    public function getFields()
103
    {
104
        if ($this->object === null) {
105
            $this->loadObject();
106
        }
107
108
        return $this->object->getFields();
109
    }
110
111
    public function getFieldsByLanguage($languageCode = null)
112
    {
113
        if ($this->object === null) {
114
            $this->loadObject();
115
        }
116
117
        return $this->object->getFieldsByLanguage($languageCode);
118
    }
119
120 View Code Duplication
    public function getField($fieldDefIdentifier, $languageCode = null)
121
    {
122
        if ($this->object === null) {
123
            $this->loadObject();
124
        }
125
126
        return $this->object->getField($fieldDefIdentifier, $languageCode);
127
    }
128
}
129