Completed
Push — master ( 64cadb...9a4f3f )
by Jelle
04:20
created

Entity   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 181
ccs 49
cts 53
cp 0.9245
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B raw() 0 23 6
A update() 0 7 3
A getEntityType() 0 4 1
A getPropertyMapDefinition() 0 7 2
A initPropertyMapDefinition() 0 1 1
A reverse() 0 4 2
A reverseArray() 0 7 2
A transform() 0 7 1
B transformHelper() 0 11 5
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Entity\EntityInterface.
5
 */
6
7
namespace TheSportsDb\Entity;
8
9
use TheSportsDb\Entity\EntityManagerInterface;
10
use TheSportsDb\Entity\EntityPropertyUtil;
11
use TheSportsDb\PropertyMapper\PropertyMapDefinition;
12
13
/**
14
 * Description of Entity
15
 *
16
 * @author drupalpro
17
 */
18
abstract class Entity implements EntityInterface {
19
20
  /**
21
   * The property map definition.
22
   *
23
   * @var \TheSportsDb\PropertyMapper\PropertyMapDefinition
24
   */
25
  protected static $propertyMapDefinition;
26
27
  /**
28
   * Creates a new Entity object.
29
   *
30
   * @param \stdClass $values
31
   *   The entity data.
32
   */
33
  public function __construct(\stdClass $values) {
34
    $this->update($values);
35
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40 1
  public function raw() {
41 1
    if (isset($this->_raw)) {
42
      return $this->_raw;
43
    }
44 1
    $this->_raw = new \stdClass();
45 1
    $reflection = new \ReflectionClass($this);
46 1
    $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
47 1
    foreach ($methods as $method) {
48
      // Skip static methods.
49 1
      if ($method->isStatic()) {
50 1
        continue;
51
      }
52 1
      $methodName = $method->getName();
53 1
      if (strpos($methodName, 'get') === 0) {
54 1
        $prop = lcfirst(substr($methodName, 3));
55 1
        if (property_exists($this, $prop)) {
56 1
          $val = $this->{$methodName}();
57 1
          $this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val);
58
        }
59
      }
60
    }
61 1
    return $this->_raw;
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67 1
  public function update(\stdClass $values) {
68 1
    foreach ((array) $values as $prop => $val) {
69 1
      if (property_exists($this, $prop)) {
70 1
        $this->{$prop} = $val;
71
      }
72
    }
73 1
  }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78 1
  public static function getEntityType() {
79 1
    $reflection = new \ReflectionClass(static::class);
80 1
    return strtolower($reflection->getShortName());
81
  }
82
83
  /**
84
   * {@inheritdoc}
85
   */
86 7
  public static function getPropertyMapDefinition() {
87 7
    if (!isset(static::$propertyMapDefinition)) {
88 3
      static::$propertyMapDefinition = new PropertyMapDefinition();
89 3
      static::initPropertyMapDefinition();
90
    }
91 7
    return static::$propertyMapDefinition;
92
  }
93
94
  /**
95
   * Initialize the property map definition for this entity type.
96
   *
97
   * @return void
98
   */
99
  protected static function initPropertyMapDefinition() {}
100
101
  /**
102
   * Reverse map a property that is an entity.
103
   *
104
   * @param array $entity
105
   *   The entity to reverse map.
106
   * @param mixed $context
107
   *   The context for this mapping. Usually the raw entity this property is
108
   *   from.
109
   * @param EntityManagerInterface $entityManager
110
   *   The entity manager.
111
   *
112
   * @return mixed
113
   *   The reverse mapped value for this property, usually the entity id.
114
   */
115 1
  public static function reverse($entity, $context, EntityManagerInterface $entityManager) {
116 1
    $data = ($entity instanceof EntityInterface) ? $entity->raw() : $entity;
117 1
    return $data->id;
118
  }
119
120
  /**
121
   * Reverse map a property that is an array of entities.
122
   *
123
   * @param array $entities
124
   *   The entities to reverse map.
125
   * @param mixed $context
126
   *   The context for this mapping. Usually the raw entity.
127
   * @param EntityManagerInterface $entityManager
128
   *   The entity manager.
129
   *
130
   * @return array
131
   *   The reverse mapped value for this property, usually an array of ids.
132
   */
133 1
  public static function reverseArray(array $entities, $context, EntityManagerInterface $entityManager) {
134 1
    $reversedEntities = array();
135 1
    foreach ($entities as $entity) {
136 1
      $reversedEntities[] = static::reverse($entity, $context, $entityManager);
137
    }
138 1
    return $reversedEntities;
139
  }
140
141
  /**
142
   * Transforms a property value to an entity.
143
   *
144
   * @param mixed $value
145
   *   The value to transform.
146
   * @param \stdClass $context
147
   *   The context for this mapping. Usually the raw entity as defined by the
148
   *   sportsdb api this property is from.
149
   * @param EntityManagerInterface $entityManager
150
   *   The entity manager.
151
   * @param string $entityType
152
   *   The enitity type to transform this value to.
153
   * @param string $idName
154
   *   The name of the identifier property as defined by the sportsdb api.
155
   * @param array $contextPropertyMap
156
   *   Extra properties to map from the context
157
   *
158
   * @return \TheSportsDb\Entity\EntityInterface
159
   *   The mapped entity.
160
   */
161 1
  public static function transform($value, $context, EntityManagerInterface $entityManager, $entityType, $idName, array $contextPropertyMap = array()) {
162 1
    $data = static::transformHelper($value, $context, $idName, $contextPropertyMap);
163 1
    $entity = $entityManager->repository($entityType)->byId($data['id']);
164
    // Update with given values.
165 1
    $entity->update($data['object']);
166 1
    return $entity;
167
  }
168
169
  /**
170
   * Helper function to transform an entity.
171
   *
172
   * @param mixed $value
173
   *   The value to transform.
174
   * @param \stdClass $context
175
   *   The context for this mapping. Usually the raw entity as defined by the
176
   *   sportsdb api this property is from.
177
   * @param string $idName
178
   *   The name of the identifier property as defined by the sportsdb api.
179
   * @param array $contextPropertyMap
180
   *   Extra properties to map from the context
181
   *
182
   * @return array
183
   *   An array with following keys:
184
   *     - object: The raw data representing the entity.
185
   *     - id: The id of the entity.
186
   */
187 1
  public static function transformHelper($value, $context, $idName, array $contextPropertyMap = array()) {
188 1
    $data = array();
189 1
    $data['id'] = is_object($value) ? $value->{$idName} : $value;
190 1
    $data['object'] = is_object($value) ? $value : (object) array($idName => $data['id']);
191 1
    foreach ($contextPropertyMap as $source => $dest) {
192 1
      if (isset($context->{$source})) {
193 1
        $data['object']->{$dest} = $context->{$source};
194
      }
195
    }
196 1
    return $data;
197
  }
198
}
199