Completed
Branch master (893650)
by Markus
04:08
created

JsonOutput::packageToJson()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 84
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 39
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 84
ccs 39
cts 39
cp 1
crap 2
rs 8.7169

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
namespace SSpkS\Output;
4
5
/**
6
 * Outputs Packages in JSON format according to
7
 * https://github.com/piwi82/Synology/wiki/Package-catalog
8
 */
9
class JsonOutput
10
{
11
    private $excludedServices = array();
12
13 2
    public function __construct()
14
    {
15 2
    }
16
17
    /**
18
     * Sets services to exclude from dependencies on output.
19
     *
20
     * @param array $excludedServices Services to exclude.
21
     */
22 1
    public function setExcludedServices($excludedServices)
23
    {
24 1
        $this->excludedServices = $excludedServices;
25 1
    }
26
27
    /**
28
     * Checks if $obj contains $property and if not, returns $alternative.
29
     *
30
     * @param object $obj Object to check
31
     * @param string $property Property to check for
32
     * @param mixed $alternative Alternative to return if key not found
33
     * @return mixed Value from $obj->$property or $alternative
34
     */
35 2
    private function ifEmpty($obj, $property, $alternative = null)
36
    {
37 2
        if (isset($obj->$property) && !empty($obj->$property)) {
38 2
            return $obj->$property;
39
        }
40 2
        return $alternative;
41
    }
42
43
    /**
44
     * Returns JSON-ready array of Package $pkg.
45
     *
46
     * @param \SSpkS\Package\Package $pkg Package
47
     * @return array JSON-ready array of $pkg.
48
     */
49 2
    private function packageToJson($pkg)
50
    {
51
/*
52
package
53
version
54
dname - displayed name
55
desc
56
price - 0
57
download_count - overall DL count
58
recent_download_count - DL count of last month?
59
link - URL
60
size
61
md5
62
thumbnail - array[URL]
63
thumbnail_retina - array[URL] (optional)
64
snapshot - array[URL] (optional)
65
qinst - true/false (optional)
66
qstart - true/false (optional)
67
qupgrade - true/false (optional)
68
depsers - "pgsql" (optional)
69
deppkgs - Pkg1>Version:Pkg2:Pkg3 (optional)
70
conflictpkgs - Pkg1<Version:Pkg2:Pkg3<Version (optional)
71
start - true/false (optional)
72
maintainer - name
73
maintainer_url - URL (optional)
74
distributor - name (optional)
75
distributor_url - URL (optional)
76
changelog - HTML
77
support_url - URL (optional)
78
thirdparty - true/false (optional)
79
category - 0-128 (bits, for multiple categories?)
80
subcategory - 0
81
type - 0 = normal, 1 = driver?, 2 = service?
82
silent_install - true/false (optional)
83
silent_uninstall - true/false (optional)
84
silent_upgrade - true/false (optional)
85
conf_deppkgs - array[Package[dsm_max_ver, pkg_min_ver]] (optional)
86
support_conf_folder - true/false (optional)
87
auto_upgrade_from - version number (optional)
88
*/
89
90 2
        if (!empty($pkg->install_dep_services)) {
91 1
            $deppkgs = trim(str_replace($this->excludedServices, '', $pkg->install_dep_services));
92 1
        } else {
93 1
            $deppkgs = null;
94
        }
95
96
        $packageJSON = array(
97 2
            'package'      => $pkg->package,
98 2
            'version'      => $pkg->version,
99 2
            'dname'        => $pkg->displayname,
100 2
            'desc'         => $pkg->description,
101 2
            'price'        => 0,
102 2
            'download_count'        => 6000, // Will only display values over 1000
103 2
            'recent_download_count' => 1222,
104 2
            'link'         => $pkg->spk_url,
105 2
            'size'         => filesize($pkg->spk),
106 2
            'md5'          => md5_file($pkg->spk),
107 2
            'thumbnail'    => $pkg->thumbnail_url,
108 2
            'snapshot'     => $pkg->snapshot_url,
109
            // quick install/start/upgrade
110 2
            'qinst'        => $this->ifEmpty($pkg, 'qinst', false),
111 2
            'qstart'       => $this->ifEmpty($pkg, 'start', false),
112 2
            'qupgrade'     => $this->ifEmpty($pkg, 'qupgrade', false),
113 2
            'depsers'      => $this->ifEmpty($pkg, 'start_dep_services'), // required started packages
114 2
            'deppkgs'      => $deppkgs,
115 2
            'conflictpkgs' => null,
116 2
            'start'        => true,
117 2
            'maintainer'      => $this->ifEmpty($pkg, 'maintainer', 'SSpkS'),
118 2
            'maintainer_url'  => $this->ifEmpty($pkg, 'maintainer_url', 'http://dummy.org/'),
119 2
            'distributor'     => $this->ifEmpty($pkg, 'distributor', 'SSpkS'),
120 2
            'distributor_url' => $this->ifEmpty($pkg, 'distributor_url', 'http://dummy.org/'),
121 2
            'changelog'    => $this->ifEmpty($pkg, 'changelog', ''),
122 2
            'thirdparty'   => true,
123 2
            'category'     => 0,
124 2
            'subcategory'  => 0,
125 2
            'type'         => 0,
126 2
            'silent_install'   => $this->ifEmpty($pkg, 'silent_install', false),
127 2
            'silent_uninstall' => $this->ifEmpty($pkg, 'silent_uninstall', false),
128 2
            'silent_upgrade'   => $this->ifEmpty($pkg, 'silent_upgrade', false),
129 2
            'beta'         => $pkg->beta, // beta channel
130 2
        );
131 2
        return $packageJSON;
132
    }
133
134
    /**
135
     * Outputs given packages as JSON.
136
     *
137
     * @param \SSpkS\Package\Package[] $pkgList List of packages to output.
138
     */
139 2
    public function outputPackages($pkgList)
140
    {
141
        $jsonOutput = array(
142 2
            'packages' => array(),
143 2
        );
144 2
        foreach ($pkgList as $pkg) {
145 2
            $pkgJson = $this->packageToJson($pkg);
146 2
            $jsonOutput['packages'][] = $pkgJson;
147 2
        }
148
149
        // Add GPG key, if it exists
150 2
        if (file_exists('./gpgkey.asc')) {
151 2
            $mygpgkey     = file_get_contents('./gpgkey.asc');
152 2
            $mygpgkey     = str_replace("\n", "\\n", $mygpgkey);
153 2
            $keyring      = array(0 => $mygpgkey);
154 2
            $jsonOutput['keyrings'] = $keyring;
155 2
        }
156
157 2
        echo stripslashes(json_encode($jsonOutput));
158 2
    }
159
}
160