Passed
Push — master ( c3d659...ab3de1 )
by Allan
03:38
created

ChangelogConfigResolver::resolveOutputTargets()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
c 0
b 0
f 0
rs 9.6111
cc 5
nc 2
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 ChangelogConfigResolver
11
{
12
    /**
13
     * @var \Composer\Package\PackageInterface
14
     */
15
    private $pluginPackage;
16
17
    /**
18
     * @var \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface
19
     */
20
    private $configExtractor;
21
22
    /**
23
     * @var \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver
24
     */
25
    private $packageInfoResolver;
26
27
    /**
28
     * @var \Vaimo\ComposerChangelogs\Composer\Plugin\Config
29
     */
30
    private $pluginConfig;
31
32
    /**
33
     * @var \Vaimo\ComposerChangelogs\Utils\PathUtils
34
     */
35
    private $pathUtils;
36
37
    /**
38
     * @param \Composer\Package\PackageInterface $pluginPackage
39
     * @param \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver
40
     * @param \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface $configExtractor
41
     */
42
    public function __construct(
43
        \Composer\Package\PackageInterface $pluginPackage,
44
        \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver,
45
        \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface $configExtractor
46
    ) {
47
        $this->pluginPackage = $pluginPackage;
48
        $this->packageInfoResolver = $packageInfoResolver;
49
        $this->configExtractor = $configExtractor;
50
51
        $this->pluginConfig = new \Vaimo\ComposerChangelogs\Composer\Plugin\Config();
52
53
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
54
    }
55
56
    public function resolveOutputTargets(PackageInterface $package)
57
    {
58
        $config = $this->getConfig($package);
59
60
        if (!isset($config['output'])) {
61
            return array();
62
        }
63
64
        $installPath = $this->packageInfoResolver->getSourcePath($package);
65
66
        return array_filter(
67
            array_map(function ($config) use ($installPath) {
68
                $path = is_array($config) ? (isset($config['path']) ? $config['path'] : '') : $config;
69
70
                if (!$path) {
71
                    return false;
72
                }
73
74
                return $installPath . DIRECTORY_SEPARATOR . $path;
75
            }, $config['output'])
76
        );
77
    }
78
79
    public function resolveOutputTemplates()
80
    {
81
        $pluginRoot = $this->packageInfoResolver->getSourcePath($this->pluginPackage);
82
        
83
        $types = $this->pluginConfig->getAvailableFormats();
84
85
        $templateGroups = array_combine(
86
            $types,
87
            array_map(function ($type) use ($pluginRoot) {
88
                return array(
89
                    'root' => array($pluginRoot, 'views', $type, 'changelog.mustache'),
90
                    'release' => array($pluginRoot, 'views', $type, 'release.mustache')
91
                );
92
            }, $types)
93
        );
94
95
        return $this->assembleGroupedFilePaths($templateGroups);
0 ignored issues
show
Bug introduced by
It seems like $templateGroups can also be of type false; however, parameter $groups of Vaimo\ComposerChangelogs...embleGroupedFilePaths() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        return $this->assembleGroupedFilePaths(/** @scrutinizer ignore-type */ $templateGroups);
Loading history...
96
    }
97
    
98
    public function resolveOutputEscapersForType($type)
99
    {
100
        $escapers = $this->pluginConfig->getEscapers();
101
102
        if (isset($escapers[$type])) {
103
            return $escapers[$type];
104
        }
105
106
        return array();
107
    }
108
    
109
    public function resolveTemplateOverrides(PackageInterface $package)
110
    {
111
        $config = $this->getConfig($package);
112
113
        $installPath = $this->packageInfoResolver->getSourcePath($package);
114
        
115
        $outputPaths = isset($config['output']) ? $config['output'] : array();
116
117
        $templateGroups = array();
118
        
119
        foreach ($outputPaths as $type => $outputConfig) {
120
            if (!is_array($outputConfig) || !isset($outputConfig['template']) || !$outputConfig['template']) {
121
                continue;
122
            }
123
124
            if (!is_array($outputConfig['template'])) {
125
                $outputConfig['template'] = array(
126
                    'root' => $outputConfig['template']
127
                );
128
            }
129
130
            $templateGroups[$type] = array_map(
131
                function ($templatePath) use ($installPath) {
132
                    return array($installPath, $templatePath);
133
                },
134
                $outputConfig['template']
135
            );
136
        }
137
138
        return $this->assembleGroupedFilePaths($templateGroups);
139
    }
140
141
    private function assembleGroupedFilePaths(array $groups)
142
    {
143
        return array_map(function (array $group) {
144
            return array_map(function (array $segments) {
145
                return implode(DIRECTORY_SEPARATOR, $segments);
146
            }, $group);
147
        }, $groups);
148
    }
149
    
150
    public function resolveSourcePath(PackageInterface $package)
151
    {
152
        $config = $this->getConfig($package);
153
154
        if (!isset($config['source'])) {
155
            return false;
156
        }
157
158
        return $this->pathUtils->composePath(
159
            $this->packageInfoResolver->getSourcePath($package),
160
            $config['source']
161
        );
162
    }
163
    
164
    public function hasConfig(PackageInterface $package)
165
    {
166
        $packageExtraConfig = $this->configExtractor->getConfig($package);
167
168
        return isset($packageExtraConfig['changelog']) && is_array($packageExtraConfig['changelog']);
169
    }
170
171
    public function getFeatureFlags(PackageInterface $package)
172
    {
173
        $config = $this->getConfig($package);
174
        
175
        return array_replace(
176
            array_fill_keys(array('links', 'dates'), true),
177
            isset($config['features']) ? $config['features'] : array()
178
        );
179
    }
180
    
181
    private function getConfig(PackageInterface $package)
182
    {
183
        $packageExtraConfig = $this->configExtractor->getConfig($package);
184
185
        if (!isset($packageExtraConfig['changelog']) || !is_array($packageExtraConfig['changelog'])) {
186
            return array();
187
        }
188
189
        return $packageExtraConfig['changelog'];
190
    }
191
}
192