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

AppConfiguration::setDirection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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
    private $splashScreens = [ ];
62
63
    /**
64
     * Returns the manifest URL.
65
     *
66
     * @return  string
67
     */
68
    public function getManifestUrl()
69
    {
70
        if ($this->manifestUrl === null) {
71
            throw new \LogicException("Manifest URL cannot be null.");
72
        }
73
74
        return $this->manifestUrl;
75
    }
76
77
    /**
78
     * Sets the manifest URL. You MUST set one for proper deployment.
79
     *
80
     * @param   string              $url
81
     *
82
     * @return  AppConfiguration
83
     */
84
    public function setManifestUrl($url)
85
    {
86
        $this->manifestUrl = $url;
87
        return $this;
88
    }
89
90
    /**
91
     * Returns the expected background color for the web application.
92
     *
93
     * @return  string|null
94
     */
95
    public function getBackgroundColor()
96
    {
97
        return $this->backgroundColor;
98
    }
99
100
    /**
101
     * Sets the expected background color for the web application. Will also be used by Chrome 47 and later to
102
     * auto-generate a splash screen.
103
     *
104
     * <p>
105
     * This value repeats what is already available in the application stylesheet, but can be used by browsers to draw
106
     * the background color of a web application when the manifest is available before the style sheet has loaded.
107
     *
108
     * <p>
109
     * This creates a smooth transition between launching the web application and loading the application's content.
110
     *
111
     * @param   string              $backgroundColor
112
     *
113
     * @return  AppConfiguration
114
     *
115
     * @see https://www.w3.org/TR/appmanifest/#background_color-member
116
     */
117
    public function setBackgroundColor($backgroundColor)
118
    {
119
        $this->backgroundColor = $backgroundColor;
120
        return $this;
121
    }
122
123
    /**
124
     * Returns the general description of what the web application does.
125
     *
126
     * @return  string|null
127
     */
128
    public function getDescription()
129
    {
130
        return $this->description;
131
    }
132
133
    /**
134
     * Sets a general description of what the web application does.
135
     *
136
     * @param   string              $description
137
     *
138
     * @return  AppConfiguration
139
     *
140
     * @see https://www.w3.org/TR/appmanifest/#description-member
141
     */
142
    public function setDescription($description)
143
    {
144
        $this->description = $description;
145
        return $this;
146
    }
147
148
    /**
149
     * Returns the primary text direction for the {@link $name}, {@link $shortName}, and {@link $description} members.
150
     *
151
     * @return  string|null
152
     */
153
    public function getDirection()
154
    {
155
        return $this->direction;
156
    }
157
158
    /**
159
     * Sets the primary text direction for the {@link $name}, {@link $shortName}, and {@link $description} members.
160
     *
161
     * @param   string              $direction          One of AppConfiguration::DIRECTION_* constants.
162
     *
163
     * @return  AppConfiguration
164
     *
165
     * @see https://www.w3.org/TR/appmanifest/#dir-member
166
     */
167 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...
168
    {
169
        if (!in_array(
170
            $direction,
171
            [
172
                self::DIRECTION_LEFT_TO_RIGHT,
173
                self::DIRECTION_RIGHT_TO_LEFT,
174
                self::DIRECTION_AUTO
175
            ]
176
        )) {
177
            throw new \InvalidArgumentException("Use one of AppConfiguration::DIRECTION_* constants.");
178
        }
179
180
        $this->direction = $direction;
181
        return $this;
182
    }
183
184
    /**
185
     * Returns the preferred display mode.
186
     *
187
     * @return  string|null
188
     */
189
    public function getDisplay()
190
    {
191
        return $this->display;
192
    }
193
194
    /**
195
     * Sets the preferred display mode.
196
     *
197
     * @param   string              $display            One of AppConfiguration::DISPLAY_* constants.
198
     *
199
     * @return  AppConfiguration
200
     *
201
     * @see https://www.w3.org/TR/appmanifest/#display-member
202
     */
203 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...
204
    {
205
        if (!in_array(
206
            $display,
207
            [
208
                self::DISPLAY_FULLSCREEN,
209
                self::DISPLAY_MINIMAL_UI,
210
                self::DISPLAY_STANDALONE,
211
                self::DISPLAY_BROWSER
212
            ]
213
        )) {
214
            throw new \InvalidArgumentException("Use one of AppConfiguration::DISPLAY_* constants.");
215
        }
216
217
        $this->display = $display;
218
        return $this;
219
    }
220
221
    /**
222
     * Returns an array of all application icons.
223
     *
224
     * @return  Image[]
225
     */
226
    public function getIcons()
227
    {
228
        return $this->icons;
229
    }
230
231
    /**
232
     * Adds an application icon.
233
     *
234
     * @param   Image               $icon
235
     *
236
     * @return  AppConfiguration
237
     *
238
     * @see https://www.w3.org/TR/appmanifest/#icons-member
239
     */
240
    public function addIcon(Image $icon)
241
    {
242
        $this->icons[] = $icon;
243
        return $this;
244
    }
245
246
    /**
247
     * Returns the application's language.
248
     *
249
     * @return  string|null
250
     */
251
    public function getLanguage()
252
    {
253
        return $this->language;
254
    }
255
256
    /**
257
     * Sets the application's language. Must be a RFC5646 compliant string.
258
     *
259
     * @param   string              $language
260
     *
261
     * @return  AppConfiguration
262
     *
263
     * @see https://www.w3.org/TR/appmanifest/#lang-member
264
     */
265
    public function setLanguage($language)
266
    {
267
        $this->language = $language;
268
        return $this;
269
    }
270
271
    /**
272
     * Returns the application's name.
273
     *
274
     * @return  string|null
275
     */
276
    public function getName()
277
    {
278
        return $this->name;
279
    }
280
281
    /**
282
     * Sets the application's name.
283
     *
284
     * @param   string              $name
285
     *
286
     * @return  AppConfiguration
287
     *
288
     * @see https://www.w3.org/TR/appmanifest/#name-member
289
     */
290
    public function setName($name)
291
    {
292
        $this->name = $name;
293
        return $this;
294
    }
295
296
    /**
297
     * Returns the default device orientation.
298
     *
299
     * @return  string|null
300
     */
301
    public function getOrientation()
302
    {
303
        return $this->orientation;
304
    }
305
306
    /**
307
     * Sets the default device orientation..
308
     *
309
     * @param   string              $orientation        One of AppConfiguration::ORIENTATION_* constants.
310
     *
311
     * @return  AppConfiguration
312
     *
313
     * @see https://www.w3.org/TR/appmanifest/#orientation-member
314
     */
315
    public function setOrientation($orientation)
316
    {
317
        if (!in_array(
318
            $orientation,
319
            [
320
                self::ORIENTATION_ANY,
321
                self::ORIENTATION_NATURAL,
322
                self::ORIENTATION_LANDSCAPE,
323
                self::ORIENTATION_LANDSCAPE_PRIMARY,
324
                self::ORIENTATION_LANDSCAPE_SECONDARY,
325
                self::ORIENTATION_PORTRAIT,
326
                self::ORIENTATION_PORTRAIT_PRIMARY,
327
                self::ORIENTATION_PORTRAIT_SECONDARY,
328
            ]
329
        )) {
330
            throw new \InvalidArgumentException("Use one of AppConfiguration::ORIENTATION_* constants.");
331
        }
332
333
        $this->orientation = $orientation;
334
        return $this;
335
    }
336
337
    /**
338
     * Returns the application's navigation scope.
339
     *
340
     * @return  string|null
341
     */
342
    public function getScope()
343
    {
344
        return $this->scope;
345
    }
346
347
    /**
348
     * Sets the application's navigation scope.
349
     *
350
     * <p>
351
     * This basically restricts what web pages can be viewed while the manifest is applied. If the user navigates the
352
     * application outside the scope, it returns to being a normal web page.
353
     *
354
     * @param   string              $scope
355
     *
356
     * @return  AppConfiguration
357
     *
358
     * @see https://www.w3.org/TR/appmanifest/#scope-member
359
     */
360
    public function setScope($scope)
361
    {
362
        $this->scope = $scope;
363
        return $this;
364
    }
365
366
    /**
367
     * Returns the application's short name.
368
     *
369
     * @return  string|null
370
     */
371
    public function getShortName()
372
    {
373
        return $this->shortName;
374
    }
375
376
    /**
377
     * Sets the application's short name.
378
     *
379
     * @param   string              $shortName
380
     *
381
     * @return  AppConfiguration
382
     *
383
     * @see https://www.w3.org/TR/appmanifest/#short_name-member
384
     */
385
    public function setShortName($shortName)
386
    {
387
        $this->shortName = $shortName;
388
        return $this;
389
    }
390
391
    /**
392
     * Returns the application's start URL.
393
     *
394
     * @return  string|null
395
     */
396
    public function getStartUrl()
397
    {
398
        return $this->startUrl;
399
    }
400
401
    /**
402
     * Sets the application's start URL.
403
     *
404
     * @param   string              $startUrl
405
     *
406
     * @return  AppConfiguration
407
     *
408
     * @see https://www.w3.org/TR/appmanifest/#start_url-member
409
     */
410
    public function setStartUrl($startUrl)
411
    {
412
        $this->startUrl = $startUrl;
413
        return $this;
414
    }
415
416
    /**
417
     * Returns the theme color.
418
     *
419
     * @return  string|null
420
     */
421
    public function getThemeColor()
422
    {
423
        return $this->themeColor;
424
    }
425
426
    /**
427
     * Sets the theme color. Will be used by Android's task switcher, for example.
428
     *
429
     * @param   string              $color
430
     *
431
     * @return  AppConfiguration
432
     *
433
     * @see https://www.w3.org/TR/appmanifest/#theme_color-member
434
     */
435
    public function setThemeColor($color)
436
    {
437
        $this->themeColor = $color;
438
        return $this;
439
    }
440
441
    /**
442
     * Returns an array of all application splash screens.
443
     *
444
     * @return  Image[]
445
     */
446
    public function getSplashScreens()
447
    {
448
        return $this->splashScreens;
449
    }
450
451
    /**
452
     * Adds an application splash screen.
453
     *
454
     * @param   Image               $splashScreen
455
     *
456
     * @return  AppConfiguration
457
     */
458
    public function addSplashScreen(Image $splashScreen)
459
    {
460
        $this->splashScreens[] = $splashScreen;
461
        return $this;
462
    }
463
464
    /**
465
     * Creates an instance of the {@link AppConfiguration} class based on the values in the provided manifest file. Use
466
     * the {@link fromManifest} method to use a JSON string as source.
467
     *
468
     * @param   string              $path               Path to a file containing an application manifest compatible
469
     *                                                  with the Web App Manifest specification.
470
     *
471
     * @return  AppConfiguration
472
     *
473
     * @see https://www.w3.org/TR/appmanifest/
474
     */
475
    public static function fromManifestFile($path)
476
    {
477
        return self::fromManifest(file_get_contents($path));
478
    }
479
480
    /**
481
     * Creates an instance of the {@link AppConfiguration} class based on the values in the provided JSON string. Use
482
     * the {@link fromManifestFile} method to use a file as source.
483
     *
484
     * @param   string              $json               A JSON string that is compatible with the Web App Manifest
485
     *                                                  specification.
486
     *
487
     * @return  AppConfiguration
488
     *
489
     * @see https://www.w3.org/TR/appmanifest/
490
     */
491
    public static function fromManifest($json)
492
    {
493
        $app = new AppConfiguration();
494
        $data = json_decode($json, true);
495
496
        if (isset($data["background_color"])) {
497
            $app->setBackgroundColor($data["background_color"]);
498
        }
499
500
        if (isset($data["description"])) {
501
            $app->setDescription($data["description"]);
502
        }
503
504
        if (isset($data["dir"])) {
505
            $app->setDirection($data["dir"]);
506
        }
507
508
        if (isset($data["display"])) {
509
            $app->setDisplay($data["display"]);
510
        }
511
512
        if (isset($data["icons"])) {
513
            foreach ($data["icons"] as $icon) {
514
                $app->addIcon(self::imageFromData($icon));
515
            }
516
        }
517
518
        if (isset($data["lang"])) {
519
            $app->setLanguage($data["lang"]);
520
        }
521
522
        if (isset($data["name"])) {
523
            $app->setName($data["name"]);
524
        }
525
526
        if (isset($data["orientation"])) {
527
            $app->setOrientation($data["orientation"]);
528
        }
529
530
        if (isset($data["scope"])) {
531
            $app->setScope($data["scope"]);
532
        }
533
534
        if (isset($data["short_name"])) {
535
            $app->setShortName($data["short_name"]);
536
        }
537
538
        if (isset($data["start_url"])) {
539
            $app->setStartUrl($data["start_url"]);
540
        }
541
542
        if (isset($data["theme_color"])) {
543
            $app->setThemeColor($data["theme_color"]);
544
        }
545
546
        if (isset($data["splash_screens"])) {
547
            foreach ($data["splash_screens"] as $splashScreen) {
548
                $app->addSplashScreen(self::imageFromData($splashScreen));
549
            }
550
        }
551
552
        return $app;
553
    }
554
555
    private static function imageFromData(array $data)
556
    {
557
        $image = new Image();
558
559
        if (isset($data["src"])) {
560
            $image->setSrc($data["src"]);
561
        }
562
563
        if (isset($data["type"])) {
564
            $image->setType($data["type"]);
565
        }
566
567
        if (isset($data["sizes"])) {
568
            $sizes = [];
569
            if (preg_match_all("/(\d+)x(\d+)/", $data["sizes"], $sizes)) {
570
                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...
571
                    $image->addSize($sizes[1][$i], $sizes[2][$i]);
572
                }
573
            }
574
        }
575
576
        return $image;
577
    }
578
}
579