Completed
Push — master ( 33966f...5c4252 )
by Jelle
03:27
created

Factory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 7
c 6
b 3
f 0
lcom 1
cbo 3
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A create() 0 11 2
A finalizeEntity() 0 6 2
A getEntityManager() 0 3 1
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Entity\Factory\Factory.
5
 */
6
7
namespace TheSportsDb\Entity\Factory;
8
9
use TheSportsDb\Entity\EntityInterface;
10
use TheSportsDb\Entity\EntityManagerConsumerTrait;
11
use TheSportsDb\Entity\EntityManagerInterface;
12
use TheSportsDb\Entity\Proxy\ProxyInterface;
13
use TheSportsDb\Http\TheSportsDbClientInterface;
14
15
/**
16
 * Default implementation of factories.
17
 *
18
 * @author Jelle Sebreghts
19
 */
20
class Factory implements FactoryInterface {
21
22
  use EntityManagerConsumerTrait;
23
24
  /**
25
   * The sports db client.
26
   *
27
   * @var \TheSportsDb\Http\TheSportsDbClientInterface
28
   */
29
  protected $sportsDbClient;
30
31
  /**
32
   * Creates a \TheSportsDb\Facotory\Factory object.
33
   *
34
   * @param \TheSportsDb\Http\TheSportsDbClientInterface $sportsDbClient
35
   *   The sports db client to make the requests.
36
   * @param \TheSportsDb\Entity\EntityManagerInterface $entityManager
0 ignored issues
show
Documentation introduced by
Should the type for parameter $entityManager not be null|EntityManagerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
   *   The entity manager.
38
   */
39
  public function __construct(TheSportsDbClientInterface $sportsDbClient, EntityManagerInterface $entityManager = NULL) {
40
    $this->sportsDbClient = $sportsDbClient;
41
    if ($entityManager instanceof EntityManagerInterface) {
42
      $this->entityManager = $entityManager;
43
    }
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function create(\stdClass $values, $entityType) {
50
    // Check if we should return a proxy or a full entity.
51
    $reflection = !$this->entityManager->isFullObject($values, $entityType) ?
52
        new \ReflectionClass($this->entityManager->getClass($entityType, 'proxy'))
53
        : new \ReflectionClass($this->entityManager->getClass($entityType));
54
55
    $entity = $reflection->newInstance($values);
56
    $this->finalizeEntity($entity);
57
58
    return $entity;
59
  }
60
61
  /**
62
   * Finalize the entity (or proxy).
63
   *
64
   * @param \TheSportsDb\Entity\EntityInterface $entity
65
   *   Either the real or the proxy entity for this factory.
66
   *
67
   * @return void
68
   */
69
  protected function finalizeEntity(EntityInterface $entity) {
70
    if ($entity instanceof ProxyInterface) {
71
      $entity->setEntityManager($this->entityManager);
72
      $entity->setSportsDbClient($this->sportsDbClient);
73
    }
74
  }
75
76
  /**
77
   * Gets the entity manager of this factory.
78
   *
79
   * @return \TheSportsDb\Entity\EntityManagerInterface
80
   *   The entity manager for this factory.
81
   */
82
  public function getEntityManager() {
83
    return $this->entityManager;
84
  }
85
86
}
87