Test Failed
Push — php7 ( 4ef047 )
by Pascal
03:45
created

JsonOutput::setExcludedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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