Completed
Push — 3.0 ( fb09f5...9f4a0e )
by Daniel
02:19
created

ParserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 11
    public function __construct(ProjectConfig $config)
28
    {
29 11
        $this->config = $config;
30 11
    }
31
32
    /**
33
     * @param PackageInterface $package
34
     * @param string $sourceDir
35
     * @return Parser
36
     * @throws \ErrorException
37
     */
38 5
    public function make(PackageInterface $package, $sourceDir)
39
    {
40 5
        $moduleSpecificMap = $this->config->getMagentoMapOverwrite();
41 5
        if (isset($moduleSpecificMap[$package->getName()])) {
42 1
            $map = $moduleSpecificMap[$package->getName()];
43 1
            return new MapParser($map);
44
        }
45
46 4
        $extra = $package->getExtra();
47 4
        if (isset($extra['map'])) {
48 1
            return new MapParser($extra['map']);
49
        }
50
51 3
        if (isset($extra['package-xml'])) {
52 1
            return new PackageXmlParser(sprintf('%s/%s', $sourceDir, $extra['package-xml']));
53
        }
54
55 2
        $modmanFile = sprintf('%s/modman', $sourceDir);
56 2
        if (file_exists($modmanFile)) {
57 1
            return new ModmanParser($modmanFile);
58
        }
59
60 1
        throw new \ErrorException(
61 1
            sprintf(
62
                'Unable to find deploy strategy for module: "%s" no known mapping'.PHP_EOL
63 1
                .'sourceDir: "%s"',
64 1
                $package->getName(),
65
                $sourceDir
66 1
            )
67 1
        );
68
    }
69
}
70