Completed
Push — master ( 66ca74...a4fc5a )
by Matthias
02:39
created

AppConfiguration::setOrientation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0011

Importance

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