Completed
Push — master ( a004d3...9bce71 )
by Hannes
03:41 queued 02:35
created

AbstractFactoryDecorator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A createId() 0 8 2
createNewInstance() 0 1 ?
1
<?php
2
3
namespace byrokrat\id;
4
5
/**
6
 * Abstract factory decorator
7
 */
8
abstract class AbstractFactoryDecorator extends IdFactory
9
{
10
    /**
11
     * @var IdFactoryInterface
12
     */
13
    private $factory;
14
15
    /**
16
     * Set factory used if this factory fails
17
     */
18 5
    public function __construct(IdFactoryInterface $factory = null)
19
    {
20 5
        $this->factory = $factory ?: new IdFactory;
21 5
    }
22
23
    /**
24
     * Create ID object from raw id string
25
     *
26
     * @param  string $raw Raw id string
27
     * @return IdInterface
28
     */
29 5
    public function createId($raw)
30
    {
31
        try {
32 5
            return $this->createNewInstance($raw);
33 1
        } catch (Exception $e) {
34 1
            return $this->factory->createId($raw);
35
        }
36
    }
37
38
    /**
39
     * Instantiate ID object
40
     *
41
     * @param  string $raw Raw id string
42
     * @return IdInterface
43
     */
44
    abstract protected function createNewInstance($raw);
45
}
46