PackageXml::convertChangeLog()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Package\PHP\Util;
38
39
use Exception;
40
use InvalidArgumentException;
41
use Pickle\Package;
42
use Pickle\Package\Util\Header;
43
use Pickle\Package\Util\JSON\Dumper;
44
45
class PackageXml
46
{
47
    protected $xmlPath;
48
49
    protected $jsonPath;
50
51
    protected $package;
52
53
    protected $versionOverride;
54
55
    public function __construct($path, $versionOverride = null)
56
    {
57
        $names = ['package2.xml', 'package.xml'];
58
59
        foreach ($names as $fl) {
60
            $xml = $path . DIRECTORY_SEPARATOR . $fl;
61
            if (is_file($xml) === true) {
62
                $this->xmlPath = $xml;
63
                break;
64
            }
65
        }
66
67
        if (!$this->xmlPath) {
68
            throw new InvalidArgumentException("The path '{$path}' doesn't contain package.xml");
69
        }
70
71
        $this->jsonPath = $path . DIRECTORY_SEPARATOR . 'composer.json';
72
        $this->versionOverride = $versionOverride;
73
    }
74
75
    public function load()
76
    {
77
        $loader = new Package\PHP\Util\XML\Loader(new Package\Util\Loader());
78
        $this->package = $loader->load($this->xmlPath, $this->versionOverride);
79
80
        if (!$this->package) {
81
            throw new Exception("Failed to load '{$this->xmlPath}'");
82
        }
83
84
        return $this->package;
85
    }
86
87
    public function convertChangeLog()
88
    {
89
        if (!$this->package) {
90
            $this->load();
91
        }
92
93
        $convertCl = new ConvertChangeLog($this->xmlPath);
94
        $convertCl->parse();
95
        $convertCl->generateReleaseFile();
96
    }
97
98
    public function dump($fname = null)
99
    {
100
        if (!$this->package) {
101
            $this->load();
102
        }
103
104
        if ($fname) {
105
            $this->jsonPath = $fname;
106
        }
107
108
        if ($this->versionOverride !== '') {
109
            $version = $this->versionOverride ?? (string) new Header\Version($this->package);
110
            if ($version !== $this->package->getPrettyVersion()) {
111
                throw new Exception("Version mismatch - '" . $version . "' != '" . $this->package->getVersion() . '. in source vs JSON');
112
            }
113
        }
114
115
        $dumper = new Dumper();
116
        $dumper->dumpToFile($this->package, $this->jsonPath, true);
117
    }
118
119
    public function getPackage()
120
    {
121
        if (!$this->package) {
122
            $this->load();
123
        }
124
125
        return $this->package;
126
    }
127
128
    public function getXmlPath()
129
    {
130
        return $this->xmlPath;
131
    }
132
133
    public function getJsonPath()
134
    {
135
        return $this->jsonPath;
136
    }
137
}
138
139
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
140