SimpleFilesystemMap::features()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage GeoDispersion
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2021-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Common\GeoDispersion\Maps;
16
17
use Brick\Geo\IO\GeoJSONReader;
18
use Brick\Geo\IO\GeoJSON\FeatureCollection;
19
use League\Flysystem\FilesystemReader;
20
use MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface;
21
use Throwable;
22
23
/**
24
 * GeoJson map defined by a single file on a file system
25
 *
26
 * @author Jonathan
27
 */
28
class SimpleFilesystemMap implements MapDefinitionInterface
29
{
30
    private string $id;
31
    private string $title;
32
    private string $path;
33
    private FilesystemReader $filesystem;
34
35
    /**
36
     * Constructor for SimpleFilesystemMap
37
     *
38
     * @param string $id
39
     * @param string $title
40
     * @param FilesystemReader $filesystem
41
     * @param string $path
42
     */
43
    public function __construct(string $id, string $title, FilesystemReader $filesystem, string $path)
44
    {
45
        $this->id = $id;
46
        $this->title = $title;
47
        $this->filesystem = $filesystem;
48
        $this->path = $path;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::id()
54
     */
55
    public function id(): string
56
    {
57
        return $this->id;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::title()
63
     */
64
    public function title(): string
65
    {
66
        return $this->title;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface::features()
72
     */
73
    public function features(): array
74
    {
75
        $reader = new GeoJSONReader();
76
        try {
77
            $feature_collection = $reader->read($this->filesystem->read($this->path));
78
            if ($feature_collection instanceof FeatureCollection) {
79
                return $feature_collection->getFeatures();
80
            }
81
        } catch (Throwable $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
82
        }
83
        return [];
84
    }
85
}
86