Passed
Push — master ( 834a40...f4d4c2 )
by Allan
02:10
created

PackageManager::bootstrapChangelogGeneration()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 65
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 65
rs 9.312
cc 4
nc 5
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Managers;
7
8
use Vaimo\ComposerChangelogs\Composer\Plugin\Config as PluginConfig;
9
use Vaimo\ComposerChangelogs\Composer\Config as ComposerConfig;
10
use Vaimo\ComposerChangelogs\Composer\Files as ComposerFiles;
11
12
class PackageManager
13
{
14
    /**
15
     * @var \Vaimo\ComposerChangelogs\Composer\Context
16
     */
17
    private $composerCtx;
18
19
    /**
20
     * @param \Vaimo\ComposerChangelogs\Composer\Context $composerCtx
21
     */
22
    public function __construct(
23
        \Vaimo\ComposerChangelogs\Composer\Context $composerCtx
24
    ) {
25
        $this->composerCtx = $composerCtx;
26
    }
27
28
    public function bootstrapChangelogGeneration(\Composer\Package\PackageInterface $package, $format = 'md')
29
    {
30
        $pluginConfig = new \Vaimo\ComposerChangelogs\Composer\Plugin\Config();
31
        
32
        $formats = $pluginConfig->getAvailableFormats();
33
        
34
        if (!isset($formats[$format])) {
35
            $message = sprintf('Unknown format: %s', $format);
36
            
37
            throw new \Vaimo\ComposerChangelogs\Exceptions\UpdaterException($message);
38
        }
39
        
40
        $composer = $this->composerCtx->getLocalComposer();
41
        
42
        $packageInfoResolver = new \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver(
43
            $composer->getInstallationManager()
44
        );
45
46
        $configExtractor = new \Vaimo\ComposerChangelogs\Extractors\VendorConfigExtractor($packageInfoResolver);
47
        
48
        $installPath = $packageInfoResolver->getInstallPath($package);
49
        $config = $configExtractor->getPackageFullConfig($package);
50
51
        $paths = array(
52
            'md' => 'CHANGELOG.md',
53
            'sphinx' => 'docs/changelog.rst',
54
            'rst' => 'docs/changelog.rst',
55
            'txt' => 'CHANGELOG.txt'
56
        );
57
58
        $filePath = isset($paths[$format]) ? $paths[$format] : sprintf('CHANGELOG.%s', $format);
59
        
60
        $update = array(
61
            ComposerConfig::CONFIG_ROOT => array(
62
                PluginConfig::ROOT => array(
63
                    'source' => 'changelog.json',
64
                    'output' => array($format => $filePath)
65
                )
66
            )
67
        );
68
69
        $pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
70
        $jsonEncoder = new \Camspiers\JsonPretty\JsonPretty();
71
72
        $encodedConfig = $jsonEncoder->prettify(array_replace_recursive($config, $update), null, '    ');
73
74
        $pkgConfigPath = $pathUtils->composePath($installPath, ComposerFiles::PACKAGE_CONFIG);
75
        $chLogConfigPath = $pathUtils->composePath($installPath, 'changelog.json');
76
77
        file_put_contents($pkgConfigPath, $encodedConfig);
78
79
        if (!file_exists($chLogConfigPath)) {
80
            $changeLog = array(
81
                '_readme' => array(
82
                    'The contents of this file are used to generate CHANGELOG.md; It\'s kept in '
83
                    . 'JSON/parsable format to make it',
84
                    'possible to generate change-logs in other formats as well (when needed) and '
85
                    . 'to do automatic releases based on',
86
                    'added change-log records. More on how to use it: https://github.com/vaimo/composer-changelogs'
87
                )
88
            );
89
90
            $encodedChangeLog = $jsonEncoder->prettify($changeLog, null, '    ');
91
92
            file_put_contents($chLogConfigPath, $encodedChangeLog);
93
        }
94
    }
95
}
96