ManifestGenerator::get()   F
last analyzed

Complexity

Conditions 16
Paths 8192

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 16

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 48
cts 48
cp 1
rs 1.4
c 0
b 0
f 0
cc 16
nc 8192
nop 1
crap 16

How to fix   Long Method    Complexity   

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
/*
4
 * This file is part of the fusonic/webapp package.
5
 *
6
 * (c) Fusonic GmbH <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fusonic\WebApp\Generators;
13
14
use Fusonic\WebApp\AppConfiguration;
15
use Fusonic\WebApp\Objects\Image;
16
17
/**
18
 * Generates an app manifest according to the W3C specification "Web App Manifest".
19
 *
20
 * @package Fusonic\WebApp
21
 *
22
 * @see https://www.w3.org/TR/appmanifest/
23
 */
24
final class ManifestGenerator
25
{
26
    /**
27
     * Returns the manifest data as an array.
28
     *
29
     * @param   AppConfiguration    $configuration      The configuration to create the manifest from.
30
     *
31
     * @return  array
32
     */
33 1
    public function get(AppConfiguration $configuration)
34
    {
35 1
        $manifest = [ ];
36
37 1
        if (($backgroundColor = $configuration->getBackgroundColor()) !== null) {
38 1
            $manifest["background_color"] = $backgroundColor;
39 1
        }
40
41 1
        if (($description = $configuration->getDescription()) !== null) {
42 1
            $manifest["description"] = $description;
43 1
        }
44
45 1
        if (($direction = $configuration->getDirection()) !== null) {
46 1
            $manifest["dir"] = $direction;
47 1
        }
48
49 1
        if (($display = $configuration->getDisplay()) !== null) {
50 1
            $manifest["display"] = $display;
51 1
        }
52
53 1
        if (count($icons = $configuration->getIcons()) > 0) {
54 1
            $manifest["icons"] = [ ];
55
56 1
            foreach ($icons as $icon) {
57 1
                $manifest["icons"][] = $this->getImageData($icon);
58 1
            }
59 1
        }
60
61 1
        if (($language = $configuration->getLanguage()) !== null) {
62 1
            $manifest["lang"] = $language;
63 1
        }
64
65 1
        if (($name = $configuration->getName()) !== null) {
66 1
            $manifest["name"] = $name;
67 1
        }
68
69 1
        if (($orientation = $configuration->getOrientation()) !== null) {
70 1
            $manifest["orientation"] = $orientation;
71 1
        }
72
73 1
        if (($scope = $configuration->getScope()) !== null) {
74 1
            $manifest["scope"] = $scope;
75 1
        }
76
77 1
        if (count($screenshots = $configuration->getScreenshots()) > 0) {
78 1
            $manifest["screenshots"] = [ ];
79
80 1
            foreach ($screenshots as $screenshot) {
81 1
                $manifest["screenshots"][] = $this->getImageData($screenshot);
82 1
            }
83 1
        }
84
85 1
        if (($shortName = $configuration->getShortName()) !== null) {
86 1
            $manifest["short_name"] = $shortName;
87 1
        }
88
89 1
        if (($startUrl = $configuration->getStartUrl()) !== null) {
90 1
            $manifest["start_url"] = $startUrl;
91 1
        }
92
93 1
        if (($themeColor = $configuration->getThemeColor()) !== null) {
94 1
            $manifest["theme_color"] = $themeColor;
95 1
        }
96
97 1
        return $manifest;
98
    }
99
100 1
    private function getImageData(Image $image)
101
    {
102
        $data = [
103 1
            "src" => $image->getSrc(),
104 1
        ];
105
106 1
        if (($platform = $image->getPlatform()) !== null) {
107
            $data["platform"] = $platform;
108
        }
109
110 1
        if (count($purpose = $image->getPurpose()) > 0) {
111
            $data["purpose"] = implode(" ", $purpose);
112
        }
113
114 1
        if (($type = $image->getType()) !== null) {
115 1
            $data["type"] = $type;
116 1
        }
117
118 1
        if (count($sizes = $image->getSizes()) > 0) {
119 1
            $data["sizes"] = implode(
120 1
                " ",
121 1
                array_map(
122 1
                    function (array $size) {
123 1
                        return "{$size[0]}x{$size[1]}";
124 1
                    },
125
                    $sizes
126 1
                )
127 1
            );
128 1
        }
129
130 1
        return $data;
131
    }
132
133
}
134