PackageManager::bootstrapChangelogGeneration()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 57
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 57
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
        $formats = $pluginConfig->getAvailableFormats();
32
        
33
        if (!isset($formats[$format])) {
34
            throw new \Vaimo\ComposerChangelogs\Exceptions\UpdaterException(
35
                sprintf('Unknown format: %s', $format)
36
            );
37
        }
38
        
39
        $composer = $this->composerCtx->getLocalComposer();
40
        $installationManager = $composer->getInstallationManager();
41
        $packageInfoResolver = new \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver($installationManager);
42
        $configExtractor = new \Vaimo\ComposerChangelogs\Extractors\VendorConfigExtractor($packageInfoResolver);
43
        $installPath = $packageInfoResolver->getInstallPath($package);
44
        $config = $configExtractor->getPackageFullConfig($package);
45
46
        $paths = array(
47
            'md' => 'CHANGELOG.md',
48
            'sphinx' => 'docs/changelog.rst',
49
            'rst' => 'docs/changelog.rst',
50
            'txt' => 'CHANGELOG.txt'
51
        );
52
53
        $filePath = isset($paths[$format]) ? $paths[$format] : sprintf('CHANGELOG.%s', $format);
54
        
55
        $update = array(
56
            ComposerConfig::CONFIG_ROOT => array(
57
                PluginConfig::ROOT => array(
58
                    'source' => 'changelog.json',
59
                    'output' => array($format => $filePath)
60
                )
61
            )
62
        );
63
64
        $pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
65
        $jsonEncoder = new \Camspiers\JsonPretty\JsonPretty();
66
        $encodedConfig = $jsonEncoder->prettify(array_replace_recursive($config, $update), null, '    ');
67
        $pkgConfigPath = $pathUtils->composePath($installPath, ComposerFiles::PACKAGE_CONFIG);
68
        $chLogConfigPath = $pathUtils->composePath($installPath, 'changelog.json');
69
70
        file_put_contents($pkgConfigPath, $encodedConfig);
71
72
        if (!file_exists($chLogConfigPath)) {
73
            $changeLog = array(
74
                '_readme' => array(
75
                    'The contents of this file are used to generate CHANGELOG.md; It\'s kept in '
76
                    . 'JSON/parsable format to make it',
77
                    'possible to generate change-logs in other formats as well (when needed) and '
78
                    . 'to do automatic releases based on',
79
                    'added change-log records. More on how to use it: https://github.com/vaimo/composer-changelogs'
80
                )
81
            );
82
83
            $encodedChangeLog = $jsonEncoder->prettify($changeLog, null, '    ');
84
            file_put_contents($chLogConfigPath, $encodedChangeLog);
85
        }
86
    }
87
}
88