Passed
Push — main ( 42b4d9...c70814 )
by Frank
01:56
created

ElementFactory::buildForGeometryObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Html\Factory;
6
7
use PrinsFrank\PhpGeoSVG\Coordinator\Coordinator;
8
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException;
9
use PrinsFrank\PhpGeoSVG\Geometry\GeometryCollection;
10
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\GeometryObject;
11
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\LineString;
12
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiLineString;
13
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiPoint;
14
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\MultiPolygon;
15
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\Point;
16
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\Polygon;
17
use PrinsFrank\PhpGeoSVG\Geometry\Position\Position;
18
use PrinsFrank\PhpGeoSVG\Html\Elements\CircleElement;
0 ignored issues
show
Bug introduced by
The type PrinsFrank\PhpGeoSVG\Html\Elements\CircleElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use PrinsFrank\PhpGeoSVG\Html\Elements\Element;
20
use PrinsFrank\PhpGeoSVG\Html\Elements\GroupElement;
0 ignored issues
show
Bug introduced by
The type PrinsFrank\PhpGeoSVG\Html\Elements\GroupElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use PrinsFrank\PhpGeoSVG\Html\Elements\PathElement;
0 ignored issues
show
Bug introduced by
The type PrinsFrank\PhpGeoSVG\Html\Elements\PathElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use PrinsFrank\PhpGeoSVG\Html\Elements\SvgElement;
0 ignored issues
show
Bug introduced by
The type PrinsFrank\PhpGeoSVG\Html\Elements\SvgElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use PrinsFrank\PhpGeoSVG\Html\Elements\Text\TextContent;
24
use PrinsFrank\PhpGeoSVG\Html\Elements\TitleElement;
25
use PrinsFrank\PhpGeoSVG\Html\Rendering\PathShapeRenderer;
26
27
class ElementFactory
28
{
29
    /**
30
     * @throws NotImplementedException
31
     */
32 2
    public static function buildForGeometryCollection(GeometryCollection $geometryCollection, Coordinator $coordinator): Element
33
    {
34 2
        $svgElement = (new SvgElement())
35 2
            ->setAttribute('width', $coordinator->getWidth())
36 2
            ->setAttribute('height', $coordinator->getHeight())
37 2
            ->setAttribute('viewbox', '0 0 ' . $coordinator->getWidth() . ' ' . $coordinator->getHeight());
38
39 2
        foreach ($geometryCollection->getGeometryObjects() as $geometryObject) {
40 1
            $svgElement->addChildElement(self::buildForGeometryObject($geometryObject, $coordinator));
41
        }
42
43 2
        return $svgElement;
44
    }
45
46
    /**
47
     * @throws NotImplementedException
48
     */
49
    public static function buildForGeometryObject(GeometryObject $geometryObject, Coordinator $coordinator): Element
50
    {
51
        $element = match (get_class($geometryObject)) {
52
            LineString::class => self::buildForLineString($geometryObject, $coordinator),
53
            MultiPoint::class => self::buildForMultiPoint($geometryObject, $coordinator),
54
            MultiPolygon::class => self::buildForMultiPolygon($geometryObject, $coordinator),
55
            MultiLineString::class => self::buildForMultiLineString($geometryObject, $coordinator),
56
            Polygon::class => self::buildForPolygon($geometryObject, $coordinator),
57
            Point::class => self::buildForPoint($geometryObject, $coordinator),
58
            default => throw new NotImplementedException('GeometryObject with class "' . get_class($geometryObject) . '" can\'t be built yet.')
59
        };
60
61
        if (null !== $geometryObject->getTitle()) {
62
            $element->addChildElement((new TitleElement())->setTextContent(new TextContent($geometryObject->getTitle())));
63
        }
64
65
        if (null !== $geometryObject->getFeatureClass()) {
66
            $element->setAttribute('data-feature-class', $geometryObject->getFeatureClass());
67
        }
68
69
        return $element;
70
    }
71
72 1
    public static function buildForLineString(LineString $lineString, Coordinator $coordinator): PathElement
73
    {
74 1
        return (new PathElement())
75 1
            ->setAttribute('d', PathShapeRenderer::renderLineStringPath($lineString, $coordinator));
76
    }
77
78
    /**
79
     * @throws NotImplementedException
80
     */
81 2
    public static function buildForMultiPoint(MultiPoint $multiPoint, Coordinator $coordinator): GroupElement
82
    {
83 2
        $element = new GroupElement();
84 2
        foreach ($multiPoint->getPoints() as $point) {
85 1
            $element->addChildElement(self::buildForGeometryObject($point, $coordinator));
86
        }
87
88 2
        return $element;
89
    }
90
91
    /**
92
     * @throws NotImplementedException
93
     */
94
    public static function buildForMultiPolygon(MultiPolygon $multiPolygon, Coordinator $coordinator): GroupElement
95
    {
96
        $element = new GroupElement();
97
        foreach ($multiPolygon->getPolygons() as $polygon) {
98
            $element->addChildElement(self::buildForGeometryObject($polygon, $coordinator));
99
        }
100
101
        return $element;
102
    }
103
104
    /**
105
     * @throws NotImplementedException
106
     */
107
    public static function buildForMultiLineString(MultiLineString $multiLineString, Coordinator $coordinator): GroupElement
108
    {
109
        $element = new GroupElement();
110
        foreach ($multiLineString->getLineStrings() as $lineString) {
111
            $element->addChildElement(self::buildForGeometryObject($lineString, $coordinator));
112
        }
113
114
        return $element;
115
    }
116
117
    /**
118
     * @throws NotImplementedException
119
     */
120
    public static function buildForPolygon(Polygon $polygon, Coordinator $coordinator): Element
121
    {
122
        if ([] === $polygon->getInteriorRings() && (null === $polygon->getFeatureClass() || null === $polygon->getExteriorRing()->getFeatureClass())) {
123
            $exteriorRing = $polygon->getExteriorRing();
124
            if (null === $exteriorRing->getFeatureClass() && null !== $polygon->getFeatureClass()) {
125
                $exteriorRing->setFeatureClass($polygon->getFeatureClass());
126
            }
127
128
            return self::buildForGeometryObject($exteriorRing, $coordinator);
129
        }
130
131
        $element = new GroupElement();
132
        $element->addChildElement(self::buildForGeometryObject($polygon->getExteriorRing(), $coordinator));
133
        foreach ($polygon->getInteriorRings() as $interiorRing) {
134
            $element->addChildElement(self::buildForGeometryObject($interiorRing, $coordinator));
135
        }
136
137
        return $element;
138
    }
139
140 1
    public static function buildForPoint(Point $point, Coordinator $coordinator): CircleElement
141
    {
142 1
        return self::buildForPosition($point->getPosition(), $coordinator);
143
    }
144
145 1
    public static function buildForPosition(Position $position, Coordinator $coordinator): CircleElement
146
    {
147 1
        $element = new CircleElement();
148 1
        $element->setAttribute('cy', $coordinator->getY($position));
149 1
        $element->setAttribute('cx', $coordinator->getX($position));
150
151 1
        return $element;
152
    }
153
}
154