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

YamlDefinitionParser::parseMigrationDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.9
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 149
    public function supports($migrationName)
21
    {
22 149
        $ext = pathinfo($migrationName, PATHINFO_EXTENSION);
23 149
        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 114
    public function parseMigrationDefinition(MigrationDefinition $definition)
33
    {
34
        try {
35 114
            $data = Yaml::parse($definition->rawDefinition);
36 1
        } catch (\Exception $e) {
37 1
            return new MigrationDefinition(
38 1
                $definition->name,
39 1
                $definition->path,
40 1
                $definition->rawDefinition,
41 1
                MigrationDefinition::STATUS_INVALID,
42 1
                array(),
43 1
                $e->getMessage()
44
            );
45
        }
46
47 113
        return $this->parseMigrationDefinitionData($data, $definition);
48
    }
49
}
50