Passed
Push — php7 ( 5be74d...d0ad73 )
by Pascal
02:05
created

JsonOutput   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 59
c 5
b 0
f 0
dl 0
loc 155
ccs 57
cts 57
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setExcludedServices() 0 3 1
A ifEmpty() 0 6 3
A __construct() 0 3 1
A packageToJson() 0 85 2
A outputPackages() 0 19 3
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 2
    public function __construct(\SSpkS\Config $config)
15
    {
16 2
        $this->config = $config;
17 2
    }
18
19
    /**
20
     * Sets services to exclude from dependencies on output.
21
     *
22
     * @param array $excludedServices Services to exclude.
23
     */
24 1
    public function setExcludedServices($excludedServices)
25
    {
26 1
        $this->excludedServices = $excludedServices;
27 1
    }
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 2
    private function ifEmpty($obj, $property, $alternative = null)
38
    {
39 2
        if (isset($obj->$property) && !empty($obj->$property)) {
40 2
            return $obj->$property;
41
        }
42 2
        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 2
    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 2
        if (!empty($pkg->install_dep_services)) {
94 1
            $deppkgs = trim(str_replace($this->excludedServices, '', $pkg->install_dep_services));
95
        } else {
96 1
            $deppkgs = null;
97
        }
98
99
        $packageJSON = array(
100 2
            'package'      => $pkg->package,
101 2
            'version'      => $pkg->version,
102 2
            'dname'        => $this->ifEmpty($pkg, 'displayname_' . $language, $pkg->displayname),
103 2
            'desc'         => $this->ifEmpty($pkg, 'description_' . $language, $pkg->description),
104 2
            'price'        => 0,
105 2
            'download_count'        => 0, // Will only display values over 1000, do not display it by default
106 2
            'recent_download_count' => 0,
107 2
            'link'         => $pkg->spk_url,
108 2
            'size'         => filesize($pkg->spk),
109 2
            'md5'          => md5_file($pkg->spk),
110 2
            'thumbnail'    => $pkg->thumbnail_url,
111 2
            'snapshot'     => $pkg->snapshot_url,
112
            // quick install/start/upgrade
113 2
            'qinst'        => $pkg->qinst,
114 2
            'qstart'       => $pkg->qstart,
115 2
            'qupgrade'     => $pkg->qupgrade,
116 2
            'depsers'      => $this->ifEmpty($pkg, 'start_dep_services'), // required started packages
117 2
            'deppkgs'      => $deppkgs,
118
            'conflictpkgs' => null,
119
            'start'        => true,
120 2
            'maintainer'      => $this->ifEmpty($pkg, 'maintainer', $this->config->packages['maintainer']),
121 2
            'maintainer_url'  => $this->ifEmpty($pkg, 'maintainer_url', $this->config->packages['maintainer_url']),
122 2
            'distributor'     => $this->ifEmpty($pkg, 'distributor', $this->config->packages['distributor']),
123 2
            'distributor_url' => $this->ifEmpty($pkg, 'distributor_url', $this->config->packages['distributor_url']),
124 2
            'support_url'  => $this->ifEmpty($pkg, 'support_url', $this->config->packages['support_url']),
125 2
            'changelog'    => $this->ifEmpty($pkg, 'changelog', ''),
126
            'thirdparty'   => true,
127 2
            'category'     => 0,
128 2
            'subcategory'  => 0,
129 2
            'type'         => 0,
130 2
            'silent_install'   => $this->ifEmpty($pkg, 'silent_install', false),
131 2
            'silent_uninstall' => $this->ifEmpty($pkg, 'silent_uninstall', false),
132 2
            'silent_upgrade'   => $this->ifEmpty($pkg, 'silent_upgrade', false),
133 2
            'auto_upgrade_from' => $this->ifEmpty($pkg, 'auto_upgrade_from'),
134 2
            'beta'         => $pkg->beta, // beta channel
135
        );
136 2
        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 2
    public function outputPackages($pkgList, $language = 'enu')
146
    {
147
        $jsonOutput = array(
148 2
            'packages' => array(),
149
        );
150 2
        foreach ($pkgList as $pkg) {
151 2
            $pkgJson = $this->packageToJson($pkg, $language);
152 2
            $jsonOutput['packages'][] = $pkgJson;
153
        }
154
155
        // Add GPG key, if it exists
156 2
        if (file_exists('./gpgkey.asc')) {
157 2
            $mygpgkey     = file_get_contents('./gpgkey.asc');
158 2
            $mygpgkey     = str_replace("\n", "\\n", $mygpgkey);
159 2
            $keyring      = array(0 => $mygpgkey);
160 2
            $jsonOutput['keyrings'] = $keyring;
161
        }
162
163 2
        echo stripslashes(json_encode($jsonOutput, JSON_UNESCAPED_UNICODE));
164 2
    }
165
}
166