Completed
Push — master ( 0add47...c5c5d3 )
by Jelle
02:57
created

Entity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
12
/**
13
 * Description of Entity
14
 *
15
 * @author drupalpro
16
 */
17
abstract class Entity implements EntityInterface {
1 ignored issue
show
Coding Style introduced by
Entity does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
18
19
  protected static $propertyMapDefinition = array();
20
21
  /**
22
   * Creates a new Entity object.
23
   *
24
   * @param \stdClass $values
25
   *   The entity data.
26
   */
27
  public function __construct(\stdClass $values) {
28
    $this->update($values);
29
  }
30
31
  public function raw() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
    if (isset($this->_raw)) {
33
      return $this->_raw;
34
    }
35
    $this->_raw = new \stdClass();
36
    $reflection = new \ReflectionClass($this);
37
    $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
38
    foreach ($methods as $method) {
39
      // Skip static methods.
40
      if ($method->isStatic()) {
41
        continue;
42
      }
43
      $methodName = $method->getName();
44
      if (strpos($methodName, 'get') === 0) {
45
        $prop = lcfirst(substr($methodName, 3));
46
        if ($reflection->hasProperty($prop)) {
47
          $val = $this->{$methodName}();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
          $this->_raw->{$prop} = EntityPropertyUtil::getRawValue($val);
49
        }
50
      }
51
    }
52
    return $this->_raw;
53
  }
54
55
  public function update(\stdClass $values) {
56
    foreach ((array) $values as $prop => $val) {
57
      if (property_exists($this, $prop)) {
58
        $this->{$prop} = $val;
59
      }
60
    }
61
  }
62
63
  public static function getEntityType() {
64
    $reflection = new \ReflectionClass(static::class);
65
    return strtolower($reflection->getShortName());
66
  }
67
68
  public static function getPropertyMapDefinition() {
69
    return static::$propertyMapDefinition;
70
  }
71
72
  public static function reverse($entity, $context, EntityManagerInterface $entityManager) {
2 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $entityManager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    $data = ($entity instanceof EntityInterface) ? $entity->raw() : $entity;
74
    return $data->id;
75
  }
76
77
  public static function reverseArray(array $entities, $context, EntityManagerInterface $entityManager) {
78
    $reversedEntities = array();
79
    foreach ($entities as $entity) {
80
      $reversedEntities[] = static::reverse($entity, $context, $entityManager);
81
    }
82
    return $reversedEntities;
83
  }
84
85
  /**
86
   * @param string $entityType
87
   * @param string $idName
88
   */
89
  public static function transform($value, $context, EntityManagerInterface $entityManager, $entityType, $idName, array $contextPropertyMap = array()) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90
    $data = static::transformHelper($value, $context, $idName, $contextPropertyMap);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
91
    $entity = $entityManager->repository($entityType)->byId($data['id']);
92
    // Update with given values.
93
    $entity->update($data['object']);
94
    return $entity;
95
  }
96
97
  public static function transformHelper($value, $context, $idName, array $contextPropertyMap = array()) {
98
    $data = array();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
    $data['id'] = is_object($value) ? $value->{$idName} : $value;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100
    $data['object'] = is_object($value) ? $value : (object) array($idName => $data['id']);
101
    foreach ($contextPropertyMap as $source => $dest) {
102
      if (isset($context->{$source})) {
103
        $data['object']->{$dest} = $context->{$source};
104
      }
105
    }
106
    return $data;
107
  }
108
}
109