JsonOutput   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 62
c 6
b 0
f 0
dl 0
loc 164
ccs 60
cts 60
cp 1
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setExcludedServices() 0 3 1
A ifEmpty() 0 6 3
A __construct() 0 3 1
A outputPackages() 0 19 3
A packageToJson() 0 92 4
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 3
    public function __construct(\SSpkS\Config $config)
15
    {
16 3
        $this->config = $config;
17 3
    }
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 3
    private function ifEmpty($obj, $property, $alternative = null)
38
    {
39 3
        if (isset($obj->$property) && !empty($obj->$property)) {
40 3
            return $obj->$property;
41
        }
42 3
        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
     * @param array $hideKeys Keys to be removed from output
52
     */
53 3
    private function packageToJson($pkg, $language, ?array $hideKeys)
54
    {
55
        /*
56
        package
57
        version
58
        dname - displayed name
59
        desc
60
        price - 0
61
        download_count - overall DL count
62
        recent_download_count - DL count of last month?
63
        link - URL
64
        size
65
        md5
66
        thumbnail - array[URL]
67
        thumbnail_retina - array[URL] (optional)
68
        snapshot - array[URL] (optional)
69
        qinst - true/false (optional)
70
        qstart - true/false (optional)
71
        qupgrade - true/false (optional)
72
        depsers - "pgsql" (optional)
73
        deppkgs - Pkg1>Version:Pkg2:Pkg3 (optional)
74
        conflictpkgs - Pkg1<Version:Pkg2:Pkg3<Version (optional)
75
        start - true/false (optional)
76
        maintainer - name
77
        maintainer_url - URL (optional)
78
        distributor - name (optional)
79
        distributor_url - URL (optional)
80
        changelog - HTML
81
        support_url - URL (optional)
82
        thirdparty - true/false (optional)
83
        category - 0-128 (bits, for multiple categories?)
84
        subcategory - 0
85
        type - 0 = normal, 1 = driver?, 2 = service?
86
        silent_install - true/false (optional)
87
        silent_uninstall - true/false (optional)
88
        silent_upgrade - true/false (optional)
89
        conf_deppkgs - array[Package[dsm_max_ver, pkg_min_ver]] (optional)
90
        support_conf_folder - true/false (optional)
91
        auto_upgrade_from - version number (optional)
92
        */
93
94 3
        if (!empty($pkg->install_dep_services)) {
95 1
            $deppkgs = trim(str_replace($this->excludedServices, '', $pkg->install_dep_services));
96
        } else {
97 2
            $deppkgs = null;
98
        }
99
100
        $packageJSON = array(
101 3
            'package' => $pkg->package,
102 3
            'version' => $pkg->version,
103 3
            'dname' => $this->ifEmpty($pkg, 'displayname_' . $language, $pkg->displayname),
104 3
            'desc' => $this->ifEmpty($pkg, 'description_' . $language, $pkg->description),
105 3
            'price' => 0,
106 3
            'download_count' => 0, // Will only display values over 1000, do not display it by default
107 3
            'recent_download_count' => 0,
108 3
            'link' => $pkg->spk_url,
109 3
            'size' => filesize($pkg->spk),
110 3
            'md5' => md5_file($pkg->spk),
111 3
            'thumbnail' => $pkg->thumbnail_url,
112 3
            'snapshot' => $pkg->snapshot_url,
113
            // quick install/start/upgrade
114 3
            'qinst' => $pkg->qinst,
115 3
            'qstart' => $pkg->qstart,
116 3
            'qupgrade' => $pkg->qupgrade,
117 3
            'depsers' => $this->ifEmpty($pkg, 'start_dep_services'), // required started packages
118 3
            'deppkgs' => $deppkgs,
119
            'conflictpkgs' => null,
120
            'start' => true,
121 3
            'maintainer' => $this->ifEmpty($pkg, 'maintainer', $this->config->packages['maintainer']),
122 3
            'maintainer_url' => $this->ifEmpty($pkg, 'maintainer_url', $this->config->packages['maintainer_url']),
123 3
            'distributor' => $this->ifEmpty($pkg, 'distributor', $this->config->packages['distributor']),
124 3
            'distributor_url' => $this->ifEmpty($pkg, 'distributor_url', $this->config->packages['distributor_url']),
125 3
            'support_url' => $this->ifEmpty($pkg, 'support_url', $this->config->packages['support_url']),
126 3
            'changelog' => $this->ifEmpty($pkg, 'changelog', ''),
127
            'thirdparty' => true,
128 3
            'category' => 0,
129 3
            'subcategory' => 0,
130 3
            'type' => 0,
131 3
            'silent_install' => $this->ifEmpty($pkg, 'silent_install', false),
132 3
            'silent_uninstall' => $this->ifEmpty($pkg, 'silent_uninstall', false),
133 3
            'silent_upgrade' => $this->ifEmpty($pkg, 'silent_upgrade', false),
134 3
            'auto_upgrade_from' => $this->ifEmpty($pkg, 'auto_upgrade_from'),
135 3
            'beta' => $pkg->beta, // beta channel
136
        );
137
138 3
        if (isset($hideKeys)) {
139 1
            foreach ($hideKeys as $hideKey) {
140 1
                unset($packageJSON[$hideKey]);
141
            }
142
        }
143
144 3
        return $packageJSON;
145
    }
146
147
    /**
148
     * Outputs given packages as JSON.
149
     *
150
     * @param \SSpkS\Package\Package[] $pkgList List of packages to output.
151
     * @param string $language The output language (this has impact on display name and description)
152
     * @param array $hideKeys Keys to be removed from output
153
     */
154 3
    public function outputPackages($pkgList, $language = 'enu', $hideKeys = null)
155
    {
156
        $jsonOutput = array(
157 3
            'packages' => array(),
158
        );
159 3
        foreach ($pkgList as $pkg) {
160 3
            $pkgJson = $this->packageToJson($pkg, $language, $hideKeys);
161 3
            $jsonOutput['packages'][] = $pkgJson;
162
        }
163
164
        // Add GPG key, if it exists
165 3
        if (file_exists('./gpgkey.asc')) {
166 3
            $mygpgkey     = file_get_contents('./gpgkey.asc');
167 3
            $mygpgkey     = str_replace("\n", "\\n", $mygpgkey);
168 3
            $keyring      = array(0 => $mygpgkey);
169 3
            $jsonOutput['keyrings'] = $keyring;
170
        }
171
172 3
        echo stripslashes(json_encode($jsonOutput, JSON_UNESCAPED_UNICODE));
173 3
    }
174
}
175