Passed
Push — main ( f71f89...a3ac80 )
by Frank
02:38
created

ElementFactory::buildForPolygon()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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