ConvertChangeLog   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 35
c 1
b 0
f 1
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A parse() 0 21 3
A generateReleaseFile() 0 20 4
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 InvalidArgumentException;
40
use RuntimeException;
41
use StdClass;
42
43
class ConvertChangeLog
44
{
45
    private $path;
46
47
    private $changelog;
48
49
    /**
50
     * @param string $path
51
     */
52
    public function __construct($path)
53
    {
54
        if (is_file($path) === false) {
55
            throw new InvalidArgumentException('File not found: ' . $path);
56
        }
57
58
        $this->path = $path;
59
    }
60
61
    public function parse()
62
    {
63
        $xml = @simplexml_load_file($this->path);
64
65
        $changelog = [];
66
        $current = new StdClass();
67
        $current->date = $xml->date;
68
        $current->time = $xml->time;
69
        $current->version = new StdClass();
70
        $current->version->release = $xml->version->release;
71
        $current->stability = new StdClass();
72
        $current->stability->release = $xml->stability->release;
73
        $current->notes = $xml->notes;
74
75
        $changelog[] = $current;
76
        if (isset($xml->changelog->release)) {
77
            foreach ($xml->changelog->release as $release) {
78
                $changelog[] = $release;
79
            }
80
        }
81
        $this->changelog = $changelog;
82
    }
83
84
    public function generateReleaseFile()
85
    {
86
        if (empty($this->changelog)) {
87
            return;
88
        }
89
90
        $contents = '';
91
        foreach ($this->changelog as $cl) {
92
            $contents .= 'Version: ' . $cl->version->release . "\n"
93
                     . 'Date: ' . $cl->date . ' ' . $cl->time . "\n"
94
                     . 'Stability: ' . $cl->stability->release . "\n"
95
                     . "\n"
96
                     . 'notes: ' . $cl->notes . "\n"
97
                     . "\n"
98
                     . "\n"
99
                     . "\n";
100
        }
101
102
        if (file_put_contents(dirname($this->path) . DIRECTORY_SEPARATOR . 'RELEASES', $contents) === false) {
103
            throw new RuntimeException('cannot save RELEASE file in <' . dirname($this->path) . DIRECTORY_SEPARATOR . 'RELEASES>');
104
        }
105
    }
106
}
107
108
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
109