Completed
Push — 3.0 ( f23b72...fb09f5 )
by Daniel
04:42
created

ParserFactory::make()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30
Metric Value
dl 0
loc 31
ccs 0
cts 27
cp 0
rs 8.439
cc 5
eloc 19
nc 5
nop 2
crap 30
1
<?php
2
3
namespace MagentoHackathon\Composer\Magento\Factory;
4
5
use Composer\Package\PackageInterface;
6
use MagentoHackathon\Composer\Magento\Parser\MapParser;
7
use MagentoHackathon\Composer\Magento\Parser\ModmanParser;
8
use MagentoHackathon\Composer\Magento\Parser\PackageXmlParser;
9
use MagentoHackathon\Composer\Magento\Parser\Parser;
10
use MagentoHackathon\Composer\Magento\ProjectConfig;
11
12
/**
13
 * Class ParserFactory
14
 * @package MagentoHackathon\Composer\Magento\Factory
15
 * @author  Aydin Hassan <[email protected]>
16
 */
17
class ParserFactory implements ParserFactoryInterface
18
{
19
20
    /**
21
     * @var ProjectConfig
22
     */
23
    protected $config;
24
25
    /**
26
     */
27
    public function __construct(ProjectConfig $config)
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * @param PackageInterface $package
34
     * @param string $sourceDir
35
     * @return Parser
36
     * @throws \ErrorException
37
     */
38
    public function make(PackageInterface $package, $sourceDir)
39
    {
40
        $moduleSpecificMap = $this->config->getMagentoMapOverwrite();
41
        if (isset($moduleSpecificMap[$package->getName()])) {
42
            $map = $moduleSpecificMap[$package->getName()];
43
            return new MapParser($map);
44
        }
45
46
        $extra = $package->getExtra();
47
        if (isset($extra['map'])) {
48
            return new MapParser($extra['map']);
49
        }
50
51
        if (isset($extra['package-xml'])) {
52
            return new PackageXmlParser(sprintf('%s/%s', $sourceDir, $extra['package-xml']));
53
        }
54
55
        $modmanFile = sprintf('%s/modman', $sourceDir);
56
        if (file_exists($modmanFile)) {
57
            return new ModmanParser($modmanFile);
58
        }
59
60
        throw new \ErrorException(
61
            sprintf(
62
                'Unable to find deploy strategy for module: "%s" no known mapping'.PHP_EOL
63
                .'sourceDir: "%s"',
64
                $package->getName(),
65
                $sourceDir
66
            )
67
        );
68
    }
69
}
70