Passed
Push — master ( 2b8f81...145d48 )
by Gaetano
10:22 queued 05:19
created

YamlDefinitionParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 36
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseMigrationDefinition() 0 16 2
A supports() 0 4 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\DefinitionParser;
4
5
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Handles Yaml migration definitions.
11
 */
12
class YamlDefinitionParser extends AbstractDefinitionParser implements DefinitionParserInterface
13
{
14
    /**
15
     * Tells whether the given file can be handled by this handler, by checking e.g. the suffix
16
     *
17
     * @param string $migrationName typically a filename
18
     * @return bool
19
     */
20
    public function supports($migrationName)
21
    {
22
        $ext = pathinfo($migrationName, PATHINFO_EXTENSION);
23
        return  $ext == 'yml' || $ext == 'yaml';
24
    }
25
26
    /**
27
     * Parses a migration definition file, and returns the list of actions to take
28
     *
29
     * @param MigrationDefinition $definition
30
     * @return MigrationDefinition
31
     */
32
    public function parseMigrationDefinition(MigrationDefinition $definition)
33
    {
34
        try {
35
            $data = Yaml::parse($definition->rawDefinition);
36
        } catch (\Exception $e) {
37
            return new MigrationDefinition(
38
                $definition->name,
39
                $definition->path,
40
                $definition->rawDefinition,
41
                MigrationDefinition::STATUS_INVALID,
42
                array(),
43
                $e->getMessage()
44
            );
45
        }
46
47
        return $this->parseMigrationDefinitionData($data, $definition);
48
    }
49
}
50