Completed
Push — master ( a004d3...9bce71 )
by Hannes
02:34
created

AbstractFactoryDecorator::createId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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