Completed
Pull Request — master (#7)
by Fabian
02:20
created

AppConfiguration::getBackgroundColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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;
13
14
use Fusonic\WebApp\Generators\ManifestGenerator;
15
use Fusonic\WebApp\Generators\TagGenerator;
16
use Fusonic\WebApp\Objects\Image;
17
18
/**
19
 * Contains all data of a web application to create different assets and/or tags.
20
 *
21
 * @package Fusonic\WebApp
22
 *
23
 * @see ManifestGenerator
24
 * @see TagGenerator
25
 */
26
final class AppConfiguration
27
{
28
    const DIRECTION_LEFT_TO_RIGHT = "ltr";
29
    const DIRECTION_RIGHT_TO_LEFT = "rtl";
30
    const DIRECTION_AUTO = "auto";
31
32
    const DISPLAY_FULLSCREEN = "fullscreen";
33
    const DISPLAY_STANDALONE = "standalone";
34
    const DISPLAY_MINIMAL_UI = "minimal-ui";
35
    const DISPLAY_BROWSER = "browser";
36
37
    const ORIENTATION_ANY = "any";
38
    const ORIENTATION_NATURAL = "natural";
39
    const ORIENTATION_LANDSCAPE = "landscape";
40
    const ORIENTATION_LANDSCAPE_PRIMARY = "landscape-primary";
41
    const ORIENTATION_LANDSCAPE_SECONDARY = "landscape-secondary";
42
    const ORIENTATION_PORTRAIT = "portrait";
43
    const ORIENTATION_PORTRAIT_PRIMARY = "portrait-primary";
44
    const ORIENTATION_PORTRAIT_SECONDARY = "portrait-secondary";
45
46
    private $backgroundColor;
47
    private $description;
48
    private $direction;
49
    private $display;
50
    private $icons = [ ];
51
    private $language;
52
    private $name;
53
    private $orientation;
54
    private $scope;
55
    private $shortName;
56
    private $startUrl;
57
    private $themeColor;
58
59
    private $manifestUrl;
60
61
    /**
62
     * Returns the manifest URL.
63
     *
64
     * @return  string
65
     */
66
    public function getManifestUrl()
67
    {
68
        if ($this->manifestUrl === null) {
69
            throw new \LogicException("Manifest URL cannot be null.");
70
        }
71
72
        return $this->manifestUrl;
73
    }
74
75
    /**
76
     * Sets the manifest URL. You MUST set one for proper deployment.
77
     *
78
     * @param   string              $url
79
     *
80
     * @return  AppConfiguration
81
     */
82
    public function setManifestUrl($url)
83
    {
84
        $this->manifestUrl = $url;
85
        return $this;
86
    }
87
88
    /**
89
     * Returns the expected background color for the web application.
90
     *
91
     * @return  string|null
92
     */
93
    public function getBackgroundColor()
94
    {
95
        return $this->backgroundColor;
96
    }
97
98
    /**
99
     * Sets the expected background color for the web application. Will also be used by Chrome 47 and later to
100
     * auto-generate a splash screen.
101
     *
102
     * <p>
103
     * This value repeats what is already available in the application stylesheet, but can be used by browsers to draw
104
     * the background color of a web application when the manifest is available before the style sheet has loaded.
105
     *
106
     * <p>
107
     * This creates a smooth transition between launching the web application and loading the application's content.
108
     *
109
     * @param   string              $backgroundColor
110
     *
111
     * @return  AppConfiguration
112
     *
113
     * @see https://www.w3.org/TR/appmanifest/#background_color-member
114
     */
115
    public function setBackgroundColor($backgroundColor)
116
    {
117
        $this->backgroundColor = $backgroundColor;
118
        return $this;
119
    }
120
121
    /**
122
     * Returns the general description of what the web application does.
123
     *
124
     * @return  string|null
125
     */
126
    public function getDescription()
127
    {
128
        return $this->description;
129
    }
130
131
    /**
132
     * Sets a general description of what the web application does.
133
     *
134
     * @param   string              $description
135
     *
136
     * @return  AppConfiguration
137
     *
138
     * @see https://www.w3.org/TR/appmanifest/#description-member
139
     */
140
    public function setDescription($description)
141
    {
142
        $this->description = $description;
143
        return $this;
144
    }
145
146
    /**
147
     * Returns the primary text direction for the {@link $name}, {@link $shortName}, and {@link $description} members.
148
     *
149
     * @return  string|null
150
     */
151
    public function getDirection()
152
    {
153
        return $this->direction;
154
    }
155
156
    /**
157
     * Sets the primary text direction for the {@link $name}, {@link $shortName}, and {@link $description} members.
158
     *
159
     * @param   string              $direction          One of AppConfiguration::DIRECTION_* constants.
160
     *
161
     * @return  AppConfiguration
162
     *
163
     * @see https://www.w3.org/TR/appmanifest/#dir-member
164
     */
165 View Code Duplication
    public function setDirection($direction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        if (!in_array(
168
            $direction,
169
            [
170
                self::DIRECTION_LEFT_TO_RIGHT,
171
                self::DIRECTION_RIGHT_TO_LEFT,
172
                self::DIRECTION_AUTO
173
            ]
174
        )) {
175
            throw new \InvalidArgumentException("Use one of AppConfiguration::DIRECTION_* constants.");
176
        }
177
178
        $this->direction = $direction;
179
        return $this;
180
    }
181
182
    /**
183
     * Returns the preferred display mode.
184
     *
185
     * @return  string|null
186
     */
187 1
    public function getDisplay()
188
    {
189 1
        return $this->display;
190
    }
191
192
    /**
193
     * Sets the preferred display mode.
194
     *
195
     * @param   string              $display            One of AppConfiguration::DISPLAY_* constants.
196
     *
197
     * @return  AppConfiguration
198
     *
199
     * @see https://www.w3.org/TR/appmanifest/#display-member
200
     */
201 1 View Code Duplication
    public function setDisplay($display)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203 1
        if (!in_array(
204 1
            $display,
205
            [
206 1
                self::DISPLAY_FULLSCREEN,
207 1
                self::DISPLAY_MINIMAL_UI,
208 1
                self::DISPLAY_STANDALONE,
209
                self::DISPLAY_BROWSER
210 1
            ]
211 1
        )) {
212
            throw new \InvalidArgumentException("Use one of AppConfiguration::DISPLAY_* constants.");
213
        }
214
215 1
        $this->display = $display;
216 1
        return $this;
217
    }
218
219
    /**
220
     * Returns an array of all application icons.
221
     *
222
     * @return  Image[]
223
     */
224 1
    public function getIcons()
225
    {
226 1
        return $this->icons;
227
    }
228
229
    /**
230
     * Adds an application icon.
231
     *
232
     * @param   Image               $icon
233
     *
234
     * @return  AppConfiguration
235
     *
236
     * @see https://www.w3.org/TR/appmanifest/#icons-member
237
     */
238 1
    public function addIcon(Image $icon)
239
    {
240 1
        $this->icons[] = $icon;
241 1
        return $this;
242
    }
243
244
    /**
245
     * Returns the application's language.
246
     *
247
     * @return  string|null
248
     */
249 1
    public function getLanguage()
250
    {
251 1
        return $this->language;
252
    }
253
254
    /**
255
     * Sets the application's language. Must be a RFC5646 compliant string.
256
     *
257
     * @param   string              $language
258
     *
259
     * @return  AppConfiguration
260
     *
261
     * @see https://www.w3.org/TR/appmanifest/#lang-member
262
     */
263 1
    public function setLanguage($language)
264
    {
265 1
        $this->language = $language;
266 1
        return $this;
267
    }
268
269
    /**
270
     * Returns the application's name.
271
     *
272
     * @return  string|null
273
     */
274 1
    public function getName()
275
    {
276 1
        return $this->name;
277
    }
278
279
    /**
280
     * Sets the application's name.
281
     *
282
     * @param   string              $name
283
     *
284
     * @return  AppConfiguration
285
     *
286
     * @see https://www.w3.org/TR/appmanifest/#name-member
287
     */
288 1
    public function setName($name)
289
    {
290 1
        $this->name = $name;
291 1
        return $this;
292
    }
293
294
    /**
295
     * Returns the default device orientation.
296
     *
297
     * @return  string|null
298
     */
299 1
    public function getOrientation()
300
    {
301 1
        return $this->orientation;
302
    }
303
304
    /**
305
     * Sets the default device orientation..
306
     *
307
     * @param   string              $orientation        One of AppConfiguration::ORIENTATION_* constants.
308
     *
309
     * @return  AppConfiguration
310
     *
311
     * @see https://www.w3.org/TR/appmanifest/#orientation-member
312
     */
313 1
    public function setOrientation($orientation)
314
    {
315 1
        if (!in_array(
316 1
            $orientation,
317
            [
318 1
                self::ORIENTATION_ANY,
319 1
                self::ORIENTATION_NATURAL,
320 1
                self::ORIENTATION_LANDSCAPE,
321 1
                self::ORIENTATION_LANDSCAPE_PRIMARY,
322 1
                self::ORIENTATION_LANDSCAPE_SECONDARY,
323 1
                self::ORIENTATION_PORTRAIT,
324 1
                self::ORIENTATION_PORTRAIT_PRIMARY,
325 1
                self::ORIENTATION_PORTRAIT_SECONDARY,
326
            ]
327 1
        )) {
328
            throw new \InvalidArgumentException("Use one of AppConfiguration::ORIENTATION_* constants.");
329
        }
330
331 1
        $this->orientation = $orientation;
332 1
        return $this;
333
    }
334
335
    /**
336
     * Returns the application's navigation scope.
337
     *
338
     * @return  string|null
339
     */
340 1
    public function getScope()
341
    {
342 1
        return $this->scope;
343
    }
344
345
    /**
346
     * Sets the application's navigation scope.
347
     *
348
     * <p>
349
     * This basically restricts what web pages can be viewed while the manifest is applied. If the user navigates the
350
     * application outside the scope, it returns to being a normal web page.
351
     *
352
     * @param   string              $scope
353
     *
354
     * @return  AppConfiguration
355
     *
356
     * @see https://www.w3.org/TR/appmanifest/#scope-member
357
     */
358 1
    public function setScope($scope)
359
    {
360 1
        $this->scope = $scope;
361 1
        return $this;
362
    }
363
364
    /**
365
     * Returns the application's short name.
366
     *
367
     * @return  string|null
368
     */
369 1
    public function getShortName()
370
    {
371 1
        return $this->shortName;
372
    }
373
374
    /**
375
     * Sets the application's short name.
376
     *
377
     * @param   string              $shortName
378
     *
379
     * @return  AppConfiguration
380
     *
381
     * @see https://www.w3.org/TR/appmanifest/#short_name-member
382
     */
383 1
    public function setShortName($shortName)
384
    {
385 1
        $this->shortName = $shortName;
386 1
        return $this;
387
    }
388
389
    /**
390
     * Returns the application's start URL.
391
     *
392
     * @return  string|null
393
     */
394 1
    public function getStartUrl()
395
    {
396 1
        return $this->startUrl;
397
    }
398
399
    /**
400
     * Sets the application's start URL.
401
     *
402
     * @param   string              $startUrl
403
     *
404
     * @return  AppConfiguration
405
     *
406
     * @see https://www.w3.org/TR/appmanifest/#start_url-member
407
     */
408 1
    public function setStartUrl($startUrl)
409
    {
410 1
        $this->startUrl = $startUrl;
411 1
        return $this;
412
    }
413
414
    /**
415
     * Returns the theme color.
416
     *
417
     * @return  string|null
418
     */
419
    public function getThemeColor()
420
    {
421
        return $this->themeColor;
422
    }
423
424
    /**
425
     * Sets the theme color. Will be used by Android's task switcher, for example.
426
     *
427
     * @param   string              $color
428
     *
429
     * @return  AppConfiguration
430
     *
431
     * @see https://www.w3.org/TR/appmanifest/#theme_color-member
432
     */
433 1
    public function setThemeColor($color)
434
    {
435 1
        $this->themeColor = $color;
436 1
        return $this;
437
    }
438
439
    /**
440
     * Creates an instance of the {@link AppConfiguration} class based on the values in the provided manifest file. Use
441
     * the {@link fromManifest} method to use a JSON string as source.
442
     *
443
     * @param   string              $path               Path to a file containing an application manifest compatible
444
     *                                                  with the Web App Manifest specification.
445
     *
446
     * @return  AppConfiguration
447
     *
448
     * @see https://www.w3.org/TR/appmanifest/
449
     */
450 1
    public static function fromManifestFile($path)
451
    {
452 1
        return self::fromManifest(file_get_contents($path));
453
    }
454
455
    /**
456
     * Creates an instance of the {@link AppConfiguration} class based on the values in the provided JSON string. Use
457
     * the {@link fromManifestFile} method to use a file as source.
458
     *
459
     * @param   string              $json               A JSON string that is compatible with the Web App Manifest
460
     *                                                  specification.
461
     *
462
     * @return  AppConfiguration
463
     *
464
     * @see https://www.w3.org/TR/appmanifest/
465
     */
466 1
    public static function fromManifest($json)
467
    {
468 1
        $app = new AppConfiguration();
469 1
        $data = json_decode($json, true);
470
471 1
        if (isset($data["background_color"])) {
472
            $app->setBackgroundColor($data["background_color"]);
473
        }
474
475 1
        if (isset($data["description"])) {
476
            $app->setDescription($data["description"]);
477
        }
478
479 1
        if (isset($data["dir"])) {
480
            $app->setDirection($data["dir"]);
481
        }
482
483 1
        if (isset($data["display"])) {
484 1
            $app->setDisplay($data["display"]);
485 1
        }
486
487 1
        if (isset($data["icons"])) {
488 1
            foreach ($data["icons"] as $icon) {
489 1
                $app->addIcon(self::imageFromData($icon));
490 1
            }
491 1
        }
492
493 1
        if (isset($data["lang"])) {
494 1
            $app->setLanguage($data["lang"]);
495 1
        }
496
497 1
        if (isset($data["name"])) {
498 1
            $app->setName($data["name"]);
499 1
        }
500
501 1
        if (isset($data["orientation"])) {
502 1
            $app->setOrientation($data["orientation"]);
503 1
        }
504
505 1
        if (isset($data["scope"])) {
506 1
            $app->setScope($data["scope"]);
507 1
        }
508
509 1
        if (isset($data["short_name"])) {
510 1
            $app->setShortName($data["short_name"]);
511 1
        }
512
513 1
        if (isset($data["start_url"])) {
514 1
            $app->setStartUrl($data["start_url"]);
515 1
        }
516
517 1
        if (isset($data["theme_color"])) {
518 1
            $app->setThemeColor($data["theme_color"]);
519 1
        }
520
521 1
        return $app;
522
    }
523
524 1
    private static function imageFromData(array $data)
525
    {
526 1
        $image = new Image();
527
528 1
        if (isset($data["src"])) {
529 1
            $image->setSrc($data["src"]);
530 1
        }
531
532 1
        if (isset($data["type"])) {
533 1
            $image->setType($data["type"]);
534 1
        }
535
536 1
        if (isset($data["sizes"])) {
537 1
            $sizes = [];
538 1
            if (preg_match_all("/(\d+)x(\d+)/", $data["sizes"], $sizes)) {
539 1
                for ($i = 0; $i < count($sizes[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
540 1
                    $image->addSize($sizes[1][$i], $sizes[2][$i]);
541 1
                }
542 1
            }
543 1
        }
544
545 1
        return $image;
546
    }
547
}
548