Passed
Push — master ( d8a10a...d39919 )
by Allan
02:25
created

ChangelogConfigResolver::assembleGroupedFilePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
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\Composer\Plugin\Config
19
     */
20
    private $pluginConfig;
21
22
    /**
23
     * @var \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface
24
     */
25
    private $configExtractor;
26
27
    /**
28
     * @var \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver
29
     */
30
    private $packageInfoResolver;
31
32
    /**
33
     * @var \Vaimo\ComposerChangelogs\Utils\PathUtils
34
     */
35
    private $pathUtils;
36
37
    /**
38
     * @param \Composer\Package\PackageInterface $pluginPackage
39
     * @param \Vaimo\ComposerChangelogs\Composer\Plugin\Config $pluginConfig
40
     * @param \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver
41
     * @param \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface $configExtractor
42
     */
43
    public function __construct(
44
        \Composer\Package\PackageInterface $pluginPackage,
45
        \Vaimo\ComposerChangelogs\Composer\Plugin\Config $pluginConfig,
46
        \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver,
47
        \Vaimo\ComposerChangelogs\Interfaces\PackageConfigExtractorInterface $configExtractor
48
    ) {
49
        $this->pluginPackage = $pluginPackage;
50
        $this->pluginConfig = $pluginConfig;
51
        $this->packageInfoResolver = $packageInfoResolver;
52
        $this->configExtractor = $configExtractor;
53
        
54
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
55
    }
56
57
    public function resolveOutputTargets(PackageInterface $package)
58
    {
59
        $config = $this->getConfig($package);
60
61
        if (!isset($config['output'])) {
62
            return array();
63
        }
64
65
        $installPath = $this->packageInfoResolver->getInstallPath($package);
66
67
        return array_filter(
68
            array_map(function ($config) use ($installPath) {
69
                $path = is_array($config) ? (isset($config['path']) ? $config['path'] : '') : $config;
70
71
                if (!$path) {
72
                    return false;
73
                }
74
75
                return $installPath . DIRECTORY_SEPARATOR . $path;
76
            }, $config['output'])
77
        );
78
    }
79
    
80
    public function resolveOutputEscapersForType($type)
81
    {
82
        $escapers = $this->pluginConfig->getEscapers();
83
84
        if (isset($escapers[$type])) {
85
            return $escapers[$type];
86
        }
87
88
        return array();
89
    }
90
    
91
    public function resolveRunnerInstallPath()
92
    {
93
        return $this->resolveInstallPath($this->pluginPackage);
94
    }
95
96
    public function resolveInstallPath(PackageInterface $package)
97
    {
98
        return $this->packageInfoResolver->getInstallPath($package);
99
    }
100
    
101
    public function resolveAvailableOutputFormats()
102
    {
103
        return $this->pluginConfig->getAvailableFormats();
104
    }
105
    
106
    public function resolveSourcePath(PackageInterface $package)
107
    {
108
        $config = $this->getConfig($package);
109
110
        if (!isset($config['source'])) {
111
            return false;
112
        }
113
114
        return $this->pathUtils->composePath(
115
            $this->packageInfoResolver->getInstallPath($package),
116
            $config['source']
117
        );
118
    }
119
    
120
    public function hasConfig(PackageInterface $package)
121
    {
122
        $packageExtraConfig = $this->configExtractor->getConfig($package);
123
124
        return isset($packageExtraConfig['changelog']) && is_array($packageExtraConfig['changelog']);
125
    }
126
127
    public function getFeatureFlags(PackageInterface $package)
128
    {
129
        $config = $this->getConfig($package);
130
        
131
        return array_replace(
132
            array_fill_keys(array('links', 'dates'), true),
133
            isset($config['features']) ? $config['features'] : array()
134
        );
135
    }
136
    
137
    public function getConfig(PackageInterface $package)
138
    {
139
        $packageExtraConfig = $this->configExtractor->getConfig($package);
140
141
        if (!isset($packageExtraConfig['changelog']) || !is_array($packageExtraConfig['changelog'])) {
142
            return array();
143
        }
144
145
        return $packageExtraConfig['changelog'];
146
    }
147
}
148