Completed
Push — master ( 2bb684...22b64a )
by Jelle
04:36
created

FactoryContainer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 41
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addFactory() 0 3 1
A getFactory() 0 6 4
A setDefaultFactory() 0 3 1
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Entity\Factory\FactoryContainer.
5
 */
6
7
namespace TheSportsDb\Entity\Factory;
8
9
/**
10
 * The default factory container implementation.
11
 *
12
 * @author Jelle Sebreghts
13
 */
14
class FactoryContainer implements FactoryContainerInterface {
15
16
  /**
17
   * Storage for factories.
18
   *
19
   * @var array
20
   */
21
  protected $factories = array();
22
23
  /**
24
   * The default factory for when no type-specific factory is registered.
25
   *
26
   * @var \TheSportsDb\Entity\Factory\FactoryInterface
27
   */
28
  protected $defaultFactory;
29
30
  /**
31
   * {@inheritdoc}
32
   */
33 1
  public function addFactory(FactoryInterface $factory, $entityType) {
34 1
    $this->factories[$entityType] = $factory;
35 1
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40 1
  public function getFactory($entityType) {
41 1
    if (!isset($this->factories[$entityType]) && !($this->defaultFactory instanceof FactoryInterface)) {
42 1
      throw new \Exception('No factory registered for ' . $entityType . ' and no default factory configured.');
43
    }
44
    return isset($this->factories[$entityType]) ? $this->factories[$entityType] : $this->defaultFactory;
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50 1
  public function setDefaultFactory(FactoryInterface $factory) {
51 1
    $this->defaultFactory = $factory;
52 1
  }
53
54
}
55