Completed
Push — master ( cc469b...cac470 )
by Jelle
10:10
created

Proxy::update()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 10
cp 0.7
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 7
nop 1
crap 6.972
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Entity\Proxy\Proxy.
5
 */
6
7
namespace TheSportsDb\Entity\Proxy;
8
9
use TheSportsDb\Entity\EntityInterface;
10
use TheSportsDb\Entity\EntityManagerConsumerTrait;
11
use TheSportsDb\Entity\EntityManagerInterface;
12
use TheSportsDb\Entity\EntityPropertyUtil;
13
use TheSportsDb\Http\TheSportsDbClientInterface;
14
15
/**
16
 * Default implementation of proxy objects.
17
 *
18
 * @author Jelle Sebreghts
19
 */
20
abstract class Proxy implements ProxyInterface {
21
22
  use EntityManagerConsumerTrait;
23
24
  /**
25
   * The sports db client.
26
   *
27
   * @var \TheSportsDb\Http\TheSportsDbClientInterface
28
   */
29
  protected $sportsDbClient;
30
31
  /**
32
   * The already available properties.
33
   *
34
   * @var \stdClass
35
   */
36
  protected $properties;
37
38
  /**
39
   * The fully loaded entity object (lazy-loaded when needed).
40
   *
41
   * @var mixed
42
   */
43
  protected $entity;
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function __construct(\stdClass $values) {
49
    $this->properties = new \stdClass();
50
    $this->update($values);
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56 1
  public function update(\stdClass $values) {
57 1
    if ($this->entity instanceof EntityInterface) {
58
      $this->entity->update($values);
59
      return;
60
    }
61 1
    foreach ((array) $values as $prop => $val) {
62 1
      if (method_exists($this, 'get' . ucfirst($prop))) {
63 1
        $this->properties->{$prop} = $val;
64
      }
65
    }
66 1
    if ($this->entityManager && $this->entityManager->isFullObject($this->properties, $this->getEntityType())) {
67
      $this->entity = $this->entityManager->factory($this->getEntityType())->create($this->properties, $this->getEntityType());
68
    }
69 1
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74 1
  public function setEntityManager(EntityManagerInterface $entityManager) {
75 1
    $this->entityManager = $entityManager;
76
    // Check if we can create a full object once the entity manager is set.
77 1
    $stub = new \stdClass();
78 1
    $this->update($stub);
79 1
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84 1
  public function setSportsDbClient(TheSportsDbClientInterface $sportsDbClient) {
85 1
    $this->sportsDbClient = $sportsDbClient;
86 1
  }
87
88
  /**
89
   * Gets a property, if it exists, loads it if necessary.
90
   *
91
   * @param string $name
92
   *   The property name.
93
   *
94
   * @return mixed
95
   *   The property value.
96
   */
97
  protected function get($name) {
98
    // If the full entity is loaded, use it.
99
    if ($this->entity instanceof EntityInterface) {
100
      return $this->entity->{'get' . ucfirst($name)}();
101
    }
102
103
    // If the property exists on the proxy, use it.
104
    if (isset($this->properties->{$name})) {
105
      return $this->properties->{$name};
106
    }
107
108
    // The property does not exist on the proxy, and the entity is not loaded in
109
    // full yet, so load it first and repeat the operation.
110
    method_exists($this, 'load' . ucfirst($name)) ? $this->{'load' . ucfirst($name)}() : $this->load();
111
    return $this->get($name);
112
113
  }
114
115
  /**
116
   * Lazy loads an entity.
117
   *
118
   * @throws \Exception
119
   *   When the entity is not found.
120
   *
121
   * @return void
122
   */
123
  abstract protected function load();
124
125
  /**
126
   * {@inheritdoc}
127
   */
128 1
  public function raw() {
129 1
    if ($this->entity) {
130
      $this->_raw = $this->entity->raw();
131
      return $this->_raw;
132
    }
133 1
    if (!isset($this->_raw)) {
134 1
      $this->_raw = new \stdClass();
135
    }
136 1
    $reflection = new \ReflectionClass($this);
137 1
    $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
138 1
    foreach ($methods as $method) {
139 1
      $methodName = $method->getName();
140 1
      if (strpos($methodName, 'get') === 0) {
141 1
        $prop = lcfirst(substr($methodName, 3));
142 1
        if (isset($this->properties->{$prop}) && !property_exists($this->_raw, $prop)) {
143 1
          $this->_raw->{$prop} = NULL;
144 1
          $val = $this->{$methodName}();
145 1
          $this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val);
146
        }
147
      }
148
    }
149 1
    return $this->_raw;
150
  }
151
152
  /**
153
   * {@inheritdoc}
154
   */
155 1
  public static function getEntityType() {
156 1
    $reflection = new \ReflectionClass(static::class);
157 1
    return strtolower(substr($reflection->getShortName(), 0, -5));
158
  }
159
160
  /**
161
   * {@inheritdoc}
162
   */
163 1
  public static function getPropertyMapDefinition() {
164 1
    $selfReflection = new \ReflectionClass(static::class);
165 1
    $namespace = substr($selfReflection->getNamespaceName(), 0, -5);
166 1
    $reflection = new \ReflectionClass($namespace . substr($selfReflection->getShortName(), 0, -5));
167 1
    return $reflection->getMethod('getPropertyMapDefinition')->invoke(NULL);
168
  }
169
}
170