Completed
Branch master (e35419)
by Gaetano
06:40
created

CustomReferenceResolver::listReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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,
13
    EnumerableReferenceResolverInterface, ContextProviderInterface
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 23
    public function getReferenceValue($identifier)
35
    {
36 23
        $identifier = $this->getReferenceIdentifier($identifier);
37 23
        if (!array_key_exists($identifier, $this->references)) {
38 1
            throw new \Exception("No reference set with identifier '$identifier'");
39
        }
40
41 22
        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 27
    public function addReference($identifier, $value, $overwrite = false)
54
    {
55 27
        if (array_key_exists($identifier, $this->references) && !$overwrite) {
56 1
            throw new \Exception("A reference with identifier '$identifier' already exists");
57
        }
58
59 27
        $this->references[$identifier] = $value;
60
61 27
        return true;
62
    }
63
64
    /**
65
     * List all existing references
66
     * @return array
67
     */
68 1
    public function listReferences()
69
    {
70 1
        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 1
    public function getCurrentContext($migrationName)
79
    {
80 1
        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