Completed
Pull Request — master (#135)
by Alex
07:30 queued 01:34
created

CustomReferenceResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 81
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A restoreContext() 0 4 1
A getReferenceValue() 0 9 2
A addReference() 0 10 3
A listReferences() 0 4 1
A getCurrentContext() 0 4 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver;
4
5
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
6
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface;
7
use Kaliop\eZMigrationBundle\API\ContextProviderInterface;
8
9
/**
10
 * Handle 'any' references by letting the developer store them and retrieve them afterwards
11
 */
12
class CustomReferenceResolver extends AbstractResolver implements ReferenceResolverBagInterface,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
13
    EnumerableReferenceResolverInterface, ContextProviderInterface
0 ignored issues
show
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
14
{
15
    /**
16
     * Defines the prefix for all reference identifier strings in definitions
17
     */
18
    protected $referencePrefixes = array('reference:');
19
20
    /**
21
     * Array of all references set by the currently running migrations.
22
     *
23
     * @var array
24
     */
25
    private $references = array();
26
27
    /**
28
     * Get a stored reference
29
     *
30
     * @param string $identifier format: reference:<some_custom_identifier>
31
     * @return mixed
32
     * @throws \Exception When trying to retrieve an unset reference
33
     */
34
    public function getReferenceValue($identifier)
35
    {
36
        $identifier = $this->getReferenceIdentifier($identifier);
37
        if (!array_key_exists($identifier, $this->references)) {
38
            throw new \Exception("No reference set with identifier '$identifier'");
39
        }
40
41
        return $this->references[$identifier];
42
    }
43
44
    /**
45
     * Add a reference to be retrieved later.
46
     *
47
     * @param string $identifier The identifier of the reference
48
     * @param mixed $value The value of the reference
49
     * @param bool $overwrite do overwrite the existing ref if it exist without raising an exception
50
     * @return bool true if the reference is accepted by this resolver, otherwise false
51
     * @throws \Exception When there is a reference with the specified $identifier already.
52
     */
53
    public function addReference($identifier, $value, $overwrite = false)
54
    {
55
        if (array_key_exists($identifier, $this->references) && !$overwrite) {
56
            throw new \Exception("A reference with identifier '$identifier' already exists");
57
        }
58
59
        $this->references[$identifier] = $value;
60
61
        return true;
62
    }
63
64
    /**
65
     * List all existing references
66
     * @return array
67
     */
68
    public function listReferences()
69
    {
70
        return $this->references;
71
    }
72
73
    /**
74
     * The custom reference resolver has only 'global' references, regardless of the current migration
75
     * @param string $migrationName
76
     * @return array|null
77
     */
78
    public function getCurrentContext($migrationName)
79
    {
80
        return $this->references;
81
    }
82
83
    /**
84
     * The custom reference resolver has only 'global' references, regardless of the current migration
85
     * @param string $migrationName
86
     * @param array $context
87
     */
88
    public function restoreContext($migrationName, array $context)
89
    {
90
        $this->references = $context;
91
    }
92
}
93