PathTranslationParser::getMappings()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 0
crap 4
1
<?php
2
3
namespace MagentoHackathon\Composer\Magento\Parser;
4
5
/**
6
 * Parser class supporting translating path mappings according to
7
 * the composer.json configuration.
8
 *
9
 * Class PathTranslationParser
10
 * @package MagentoHackathon\Composer\Magento\Parser
11
 */
12
class PathTranslationParser implements Parser
13
{
14
    /**
15
     * @var array Variants on each prefix that path mappings are checked
16
     * against.
17
     */
18
    protected $pathPrefixVariants = array('', './');
19
20
    /**
21
     * @var array Path mapping prefixes that need to be translated (i.e. to
22
     * use a public directory as the web server root).
23
     */
24
    protected $pathPrefixTranslations = array();
25
26
    /**
27
     * @var Parser
28
     */
29
    protected $parser;
30
31
    /**
32
     * Constructor. Sets the list of path translations to use.
33
     *
34
     * @param Parser $parser
35
     * @param array $translations Path translations
36
     */
37 2
    public function __construct(Parser $parser, array $translations)
38
    {
39 2
        $this->pathPrefixTranslations = $this->createPrefixVariants($translations);
40 2
        $this->parser = $parser;
41 2
    }
42
43
    /**
44
     * Given an array of path mapping translations, combine them with a list
45
     * of starting variations. This is so that a translation for 'js' will
46
     * also match path mappings beginning with './js'.
47
     *
48
     * @param $translations
49
     * @return array
50
     */
51 2
    protected function createPrefixVariants($translations)
52
    {
53 2
        $newTranslations = array();
54 2
        foreach ($translations as $key => $value) {
55 1
            foreach ($this->pathPrefixVariants as $variant) {
56 1
                $newTranslations[$variant . $key] = $value;
57
            }
58
        }
59
60 2
        return $newTranslations;
61
    }
62
63
    /**
64
     * loop the mappings for the wrapped parser, check if any of the targets are for
65
     * directories that have been moved under the public directory. If so,
66
     * update the target paths to include 'public/'. As no standard Magento
67
     * path mappings should ever start with 'public/', and  path mappings
68
     * that already include the public directory should always have
69
     * js/skin/media paths starting with 'public/', it should be safe to call
70
     * multiple times on either.
71
     *
72
     * @return array Updated path mappings
73
     */
74 1
    public function getMappings()
75
    {
76 1
        $translatedMappings = array();
77 1
        foreach ($this->parser->getMappings() as $index => $mapping) {
78 1
            $translatedMappings[$index] = $mapping;
79 1
            foreach ($this->pathPrefixTranslations as $prefix => $translate) {
80 1
                if (strpos($mapping[1], $prefix) === 0) {
81
                    // replace the old prefix with the translated version
82 1
                    $translatedMappings[$index][1] = $translate . substr($mapping[1], strlen($prefix));
83
                    // should never need to translate a prefix more than once
84
                    // per path mapping
85 1
                    break;
86
                }
87
            }
88
        }
89
90 1
        return $translatedMappings;
91
    }
92
}
93