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

ContextHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 53
ccs 12
cts 22
cp 0.5455
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addProvider() 0 3 1
A deleteContext() 0 3 1
A storeCurrentContext() 0 8 2
A restoreCurrentContext() 0 9 4
A __construct() 0 3 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core;
4
5
use Kaliop\eZMigrationBundle\API\ContextStorageHandlerInterface;
6
use Kaliop\eZMigrationBundle\API\ContextProviderInterface;
7
8
/**
9
 * Takes care of storing / restoring all necessary migration execution context information, acting as a "multiplexer":
10
 * services which hold context information have to implement the ContextProviderInterface interface
11
 */
12
class ContextHandler
13
{
14
    protected $storageHandler;
15
    /** @var ContextProviderInterface[] $providers  */
16
    protected $providers = array();
17
18 1
    public function __construct(ContextStorageHandlerInterface $storageHandler)
19
    {
20 1
        $this->storageHandler = $storageHandler;
21 1
    }
22
23
    /**
24
     * @param ContextProviderInterface $contextProvider
25
     * @param string $label
26
     */
27 1
    public function addProvider(ContextProviderInterface $contextProvider, $label)
28
    {
29 1
        $this->providers[$label] = $contextProvider;
30 1
    }
31
32
    /**
33
     * @param string $migrationName
34
     */
35 1
    public function storeCurrentContext($migrationName)
36
    {
37 1
        $context = array();
38 1
        foreach($this->providers as $label => $provider) {
39 1
            $context[$label] = $provider->getCurrentContext($migrationName);
40
        }
41
42 1
        $this->storageHandler->storeMigrationContext($migrationName, $context);
43 1
    }
44
45
    /**
46
     * @param string $migrationName
47
     * @throws \Exception
48
     */
49
    public function restoreCurrentContext($migrationName)
50
    {
51
        $context = $this->storageHandler->loadMigrationContext($migrationName);
52
        if (!is_array($context)) {
53
            throw new \Exception("No execution context found associated with migration '$migrationName'");
54
        }
55
        foreach($this->providers as $label => $provider) {
56
            if (isset($context[$label])) {
57
                $provider->restoreContext($migrationName, $context[$label]);
58
            }
59
        }
60
    }
61
62
    public function deleteContext($migrationName)
63
    {
64
        $this->storageHandler->deleteMigrationContext($migrationName);
65
    }
66
}
67