Passed
Branch master (b0d099)
by Markus
03:49
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 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 39
c 4
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
    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
     * @return array JSON-ready array of $pkg.
50
     */
51 2
    private function packageToJson($pkg)
52
    {
53
/*
54
package
55
version
56
dname - displayed name
57
desc
58
price - 0
59
download_count - overall DL count
60
recent_download_count - DL count of last month?
61
link - URL
62
size
63
md5
64
thumbnail - array[URL]
65
thumbnail_retina - array[URL] (optional)
66
snapshot - array[URL] (optional)
67
qinst - true/false (optional)
68
qstart - true/false (optional)
69
qupgrade - true/false (optional)
70
depsers - "pgsql" (optional)
71
deppkgs - Pkg1>Version:Pkg2:Pkg3 (optional)
72
conflictpkgs - Pkg1<Version:Pkg2:Pkg3<Version (optional)
73
start - true/false (optional)
74
maintainer - name
75
maintainer_url - URL (optional)
76
distributor - name (optional)
77
distributor_url - URL (optional)
78
changelog - HTML
79
support_url - URL (optional)
80
thirdparty - true/false (optional)
81
category - 0-128 (bits, for multiple categories?)
82
subcategory - 0
83
type - 0 = normal, 1 = driver?, 2 = service?
84
silent_install - true/false (optional)
85
silent_uninstall - true/false (optional)
86
silent_upgrade - true/false (optional)
87
conf_deppkgs - array[Package[dsm_max_ver, pkg_min_ver]] (optional)
88
support_conf_folder - true/false (optional)
89
auto_upgrade_from - version number (optional)
90
*/
91
92 2
        if (!empty($pkg->install_dep_services)) {
93 1
            $deppkgs = trim(str_replace($this->excludedServices, '', $pkg->install_dep_services));
94 1
        } else {
95 1
            $deppkgs = null;
96
        }
97
98
        $packageJSON = array(
99 2
            'package'      => $pkg->package,
100 2
            'version'      => $pkg->version,
101 2
            'dname'        => $pkg->displayname,
102 2
            'desc'         => $pkg->description,
103 2
            'price'        => 0,
104 2
            'download_count'        => 0, // Will only display values over 1000, do not display it by default
105 2
            'recent_download_count' => 0,
106 2
            'link'         => $pkg->spk_url,
107 2
            'size'         => filesize($pkg->spk),
108 2
            'md5'          => md5_file($pkg->spk),
109 2
            'thumbnail'    => $pkg->thumbnail_url,
110 2
            'snapshot'     => $pkg->snapshot_url,
111
            // quick install/start/upgrade
112 2
            'qinst'        => $this->ifEmpty($pkg, 'qinst', false),
113 2
            'qstart'       => $this->ifEmpty($pkg, 'start', false),
114 2
            'qupgrade'     => $this->ifEmpty($pkg, 'qupgrade', false),
115 2
            'depsers'      => $this->ifEmpty($pkg, 'start_dep_services'), // required started packages
116 2
            'deppkgs'      => $deppkgs,
117 2
            'conflictpkgs' => null,
118 2
            'start'        => true,
119 2
            'maintainer'      => $this->ifEmpty($pkg, 'maintainer', $this->config->packages['maintainer']),
0 ignored issues
show
Documentation introduced by
The property packages does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120 2
            'maintainer_url'  => $this->ifEmpty($pkg, 'maintainer_url', $this->config->packages['maintainer_url']),
0 ignored issues
show
Documentation introduced by
The property packages does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121 2
            'distributor'     => $this->ifEmpty($pkg, 'distributor', $this->config->packages['distributor']),
0 ignored issues
show
Documentation introduced by
The property packages does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122 2
            'distributor_url' => $this->ifEmpty($pkg, 'distributor_url', $this->config->packages['distributor_url']),
0 ignored issues
show
Documentation introduced by
The property packages does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123 2
            'changelog'    => $this->ifEmpty($pkg, 'changelog', ''),
124 2
            'thirdparty'   => true,
125 2
            'category'     => 0,
126 2
            'subcategory'  => 0,
127 2
            'type'         => 0,
128 2
            'silent_install'   => $this->ifEmpty($pkg, 'silent_install', false),
129 2
            'silent_uninstall' => $this->ifEmpty($pkg, 'silent_uninstall', false),
130 2
            'silent_upgrade'   => $this->ifEmpty($pkg, 'silent_upgrade', false),
131 2
            'beta'         => $pkg->beta, // beta channel
132 2
        );
133 2
        return $packageJSON;
134
    }
135
136
    /**
137
     * Outputs given packages as JSON.
138
     *
139
     * @param \SSpkS\Package\Package[] $pkgList List of packages to output.
140
     */
141 2
    public function outputPackages($pkgList)
142
    {
143
        $jsonOutput = array(
144 2
            'packages' => array(),
145 2
        );
146 2
        foreach ($pkgList as $pkg) {
147 2
            $pkgJson = $this->packageToJson($pkg);
148 2
            $jsonOutput['packages'][] = $pkgJson;
149 2
        }
150
151
        // Add GPG key, if it exists
152 2
        if (file_exists('./gpgkey.asc')) {
153 2
            $mygpgkey     = file_get_contents('./gpgkey.asc');
154 2
            $mygpgkey     = str_replace("\n", "\\n", $mygpgkey);
155 2
            $keyring      = array(0 => $mygpgkey);
156 2
            $jsonOutput['keyrings'] = $keyring;
157 2
        }
158
159 2
        echo stripslashes(json_encode($jsonOutput, JSON_UNESCAPED_UNICODE));
160 2
    }
161
}
162