Completed
Pull Request — master (#31)
by
unknown
02:34
created

JsonOutput::ifUnset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 2
    private function ifUnset($obj, $property, $alternative = null)
43
    {
44 2
        if (isset($obj->$property)) {
45 2
            return $obj->$property;
46
        }
47 2
        return $alternative;
48
    }
49
50
    /**
51
     * Returns JSON-ready array of Package $pkg.
52
     *
53
     * @param \SSpkS\Package\Package $pkg Package
54
     * @return array JSON-ready array of $pkg.
55
     */
56 2
    private function packageToJson($pkg)
57
    {
58
/*
59
package
60
version
61
dname - displayed name
62
desc
63
price - 0
64
download_count - overall DL count
65
recent_download_count - DL count of last month?
66
link - URL
67
size
68
md5
69
thumbnail - array[URL]
70
thumbnail_retina - array[URL] (optional)
71
snapshot - array[URL] (optional)
72
qinst - true/false (optional)
73
qstart - true/false (optional)
74
qupgrade - true/false (optional)
75
depsers - "pgsql" (optional)
76
deppkgs - Pkg1>Version:Pkg2:Pkg3 (optional)
77
conflictpkgs - Pkg1<Version:Pkg2:Pkg3<Version (optional)
78
start - true/false (optional)
79
maintainer - name
80
maintainer_url - URL (optional)
81
distributor - name (optional)
82
distributor_url - URL (optional)
83
changelog - HTML
84
support_url - URL (optional)
85
thirdparty - true/false (optional)
86
category - 0-128 (bits, for multiple categories?)
87
subcategory - 0
88
type - 0 = normal, 1 = driver?, 2 = service?
89
silent_install - true/false (optional)
90
silent_uninstall - true/false (optional)
91
silent_upgrade - true/false (optional)
92
conf_deppkgs - array[Package[dsm_max_ver, pkg_min_ver]] (optional)
93
support_conf_folder - true/false (optional)
94
auto_upgrade_from - version number (optional)
95
*/
96
97 2
        if (!empty($pkg->install_dep_services)) {
98 1
            $deppkgs = trim(str_replace($this->excludedServices, '', $pkg->install_dep_services));
99 1
        } else {
100 1
            $deppkgs = null;
101
        }
102
103
        $packageJSON = array(
104 2
            'package'      => $pkg->package,
105 2
            'version'      => $pkg->version,
106 2
            'dname'        => $pkg->displayname,
107 2
            'desc'         => $pkg->description,
108 2
            'price'        => 0,
109 2
            'download_count'        => 6000, // Will only display values over 1000
110 2
            'recent_download_count' => 1222,
111 2
            'link'         => $pkg->spk_url,
112 2
            'size'         => filesize($pkg->spk),
113 2
            'md5'          => md5_file($pkg->spk),
114 2
            'thumbnail'    => $pkg->thumbnail_url,
115 2
            'snapshot'     => $pkg->snapshot_url,
116
            // quick install/start/upgrade
117 2
            'qinst'        => $this->ifEmpty($pkg, 'qinst', false),
118 2
            'qstart'       => $this->ifEmpty($pkg, 'start', false),
119 2
            'qupgrade'     => $this->ifEmpty($pkg, 'qupgrade', false),
120 2
            'depsers'      => $this->ifEmpty($pkg, 'start_dep_services'), // required started packages
121 2
            'deppkgs'      => $deppkgs,
122 2
            'conflictpkgs' => null,
123 2
            'start'        => true,
124 2
            'maintainer'      => $this->ifUnset($pkg, 'maintainer', 'SSpkS'),
125 2
            'maintainer_url'  => $this->ifUnset($pkg, 'maintainer_url', 'http://dummy.org/'),
126 2
            'distributor'     => $this->ifUnset($pkg, 'distributor', 'SSpkS'),
127 2
            'distributor_url' => $this->ifUnset($pkg, 'distributor_url', 'http://dummy.org/'),
128 2
            'changelog'    => $this->ifEmpty($pkg, 'changelog', ''),
129 2
            'thirdparty'   => true,
130 2
            'category'     => 0,
131 2
            'subcategory'  => 0,
132 2
            'type'         => 0,
133 2
            'silent_install'   => $this->ifEmpty($pkg, 'silent_install', false),
134 2
            'silent_uninstall' => $this->ifEmpty($pkg, 'silent_uninstall', false),
135 2
            'silent_upgrade'   => $this->ifEmpty($pkg, 'silent_upgrade', false),
136 2
            'beta'         => $pkg->beta, // beta channel
137 2
        );
138 2
        return $packageJSON;
139
    }
140
141
    /**
142
     * Outputs given packages as JSON.
143
     *
144
     * @param \SSpkS\Package\Package[] $pkgList List of packages to output.
145
     */
146 2
    public function outputPackages($pkgList)
147
    {
148
        $jsonOutput = array(
149 2
            'packages' => array(),
150 2
        );
151 2
        foreach ($pkgList as $pkg) {
152 2
            $pkgJson = $this->packageToJson($pkg);
153 2
            $jsonOutput['packages'][] = $pkgJson;
154 2
        }
155
156
        // Add GPG key, if it exists
157 2
        if (file_exists('./gpgkey.asc')) {
158 2
            $mygpgkey     = file_get_contents('./gpgkey.asc');
159 2
            $mygpgkey     = str_replace("\n", "\\n", $mygpgkey);
160 2
            $keyring      = array(0 => $mygpgkey);
161 2
            $jsonOutput['keyrings'] = $keyring;
162 2
        }
163
164 2
        echo stripslashes(json_encode($jsonOutput));
165 2
    }
166
}
167