resolveTemplateOverrides()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 9.4555
cc 5
nc 8
nop 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Resolvers;
7
8
use Composer\Package\PackageInterface;
9
10
class ChangelogTemplateResolver
11
{
12
    /**
13
     * @var \Vaimo\ComposerChangelogs\Resolvers\ChangelogConfigResolver
14
     */
15
    private $configResolver;
16
17
    /**
18
     * @param \Vaimo\ComposerChangelogs\Resolvers\ChangelogConfigResolver $configResolver
19
     */
20
    public function __construct(
21
        \Vaimo\ComposerChangelogs\Resolvers\ChangelogConfigResolver $configResolver
22
    ) {
23
        $this->configResolver = $configResolver;
24
    }
25
26
    public function getTemplates(PackageInterface $package)
27
    {
28
        return array_replace_recursive(
29
            $this->resolveOutputTemplates(),
30
            $this->resolveTemplateOverrides($package)
31
        );
32
    }
33
    
34
    private function resolveOutputTemplates()
35
    {
36
        $outputFormats = $this->configResolver->resolveAvailableOutputFormats();
37
38
        $pluginRoot = $this->configResolver->resolveRunnerInstallPath();
39
40
        $templateGroups = array_combine(
41
            $outputFormats,
42
            array_map(function ($type) use ($pluginRoot) {
43
                return array(
44
                    'root' => array($pluginRoot, 'views', $type, 'changelog.mustache'),
45
                    'release' => array($pluginRoot, 'views', $type, 'release.mustache')
46
                );
47
            }, $outputFormats)
48
        );
49
50
        if ($templateGroups === false) {
51
            return array();
52
        }
53
54
        return $this->assembleGroupedFilePaths($templateGroups);
55
    }
56
57
    private function resolveTemplateOverrides(PackageInterface $package)
58
    {
59
        $config = $this->configResolver->getConfig($package);
60
61
        $installPath = $this->configResolver->resolveInstallPath($package);
62
63
        $outputPaths = isset($config['output']) ? $config['output'] : array();
64
65
        $templateGroups = array();
66
67
        foreach ($outputPaths as $type => $outputConfig) {
68
            if (!$this->hasTemplate($outputConfig)) {
69
                continue;
70
            }
71
72
            if (!is_array($outputConfig['template'])) {
73
                $outputConfig['template'] = array(
74
                    'root' => $outputConfig['template']
75
                );
76
            }
77
78
            $templateGroups[$type] = array_map(
79
                function ($templatePath) use ($installPath) {
80
                    return array($installPath, $templatePath);
81
                },
82
                $outputConfig['template']
83
            );
84
        }
85
86
        return $this->assembleGroupedFilePaths($templateGroups);
87
    }
88
    
89
    private function hasTemplate($config)
90
    {
91
        return is_array($config) && isset($config['template']) && !$config['template'];
92
    }
93
94
    private function assembleGroupedFilePaths(array $groups)
95
    {
96
        return array_map(function (array $group) {
97
            return array_map(function (array $segments) {
98
                return implode(DIRECTORY_SEPARATOR, $segments);
99
            }, $group);
100
        }, $groups);
101
    }
102
}
103