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

YamlDefinitionParser::parseMigrationDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 15
cp 0
crap 6
rs 9.9
c 0
b 0
f 0
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