Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

SimpleFilesystemMap::features()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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