Passed
Branch main (a5ccba)
by Gaetano
04:40
created

AbstractDefinitionParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 56
ccs 26
cts 26
cp 1
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B parseMigrationDefinitionData() 0 46 7
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\DefinitionParser;
4
5
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
7
8
class AbstractDefinitionParser
9
{
10
    /**
11
     * Parses a migration definition in the form of an array of steps
12
     *
13
     * @param array $data
14
     * @param MigrationDefinition $definition
15
     * @param string $format
16
     * @return MigrationDefinition
17
     */
18 132
    protected function parseMigrationDefinitionData($data, MigrationDefinition $definition, $format = 'Yaml')
19
    {
20
        // basic validation
21
22
        /// @todo move to using the Validator component...
23
24 132
        $status = MigrationDefinition::STATUS_PARSED;
25
26 132
        if (!is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
27 1
            $status = MigrationDefinition::STATUS_INVALID;
28 1
            $message = "$format migration file '{$definition->path}' must contain an array as top element";
29
        } else {
30 131
            foreach ($data as $i => $stepDef) {
31 128
                if (!isset($stepDef['type']) || !is_string($stepDef['type'])) {
32 2
                    $status = MigrationDefinition::STATUS_INVALID;
33 2
                    $message = "$format migration file '{$definition->path}' misses or has a non-string 'type' element in step $i";
34 2
                    break;
35
                }
36
            }
37
        }
38
39 132
        if ($status != MigrationDefinition::STATUS_PARSED)
40
        {
41 3
            return new MigrationDefinition(
42 3
                $definition->name,
43 3
                $definition->path,
44 3
                $definition->rawDefinition,
45
                $status,
46 3
                array(),
47
                $message
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
48
            );
49
        }
50
51 129
        $stepDefs = array();
52 129
        foreach ($data as $stepDef) {
53 126
            $type = $stepDef['type'];
54 126
            unset($stepDef['type']);
55 126
            $stepDefs[] = new MigrationStep($type, $stepDef, array('path' => $definition->path));
56
        }
57
58 129
        return new MigrationDefinition(
59 129
            $definition->name,
60 129
            $definition->path,
61 129
            $definition->rawDefinition,
62 129
            MigrationDefinition::STATUS_PARSED,
63
            $stepDefs
64
        );
65
    }
66
}
67