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

ContextHandler::__construct()   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 1
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;
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