Completed
Push — master ( e0022c...a004d3 )
by Hannes
02:13 queued 01:15
created

Factory::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\Component;
4
5
use byrokrat\id\IdFactory;
6
use byrokrat\id\IdInterface;
7
use byrokrat\id\Exception;
8
9
/**
10
 * Abstract factory implementation
11
 */
12
trait Factory
13
{
14
    /**
15
     * @var IdFactory Fallback factory
16
     */
17
    private $factory;
18
19
    /**
20
     * Set factory used if this factory fails
21
     */
22 5
    public function __construct(IdFactory $factory = null)
23
    {
24 5
        $this->factory = $factory ?: new IdFactory;
25 5
    }
26
27
    /**
28
     * Create ID object from raw id string
29
     *
30
     * @param  string $raw Raw id string
31
     * @return IdInterface
32
     */
33 5
    public function createId($raw)
34
    {
35
        try {
36 5
            return $this->createNewInstance($raw);
37 1
        } catch (Exception $e) {
38 1
            return $this->factory->createId($raw);
39
        }
40
    }
41
42
    /**
43
     * Instantiate ID object
44
     *
45
     * @param  string $raw Raw id string
46
     * @return IdInterface
47
     */
48
    abstract protected function createNewInstance($raw);
49
}
50