Completed
Push — development ( 2fa2b9...07d500 )
by Thomas
23s
created

GeoCacheService::fetchByWaypoint()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 9
nop 1
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\GeoCache\Persistence\GeoCache;
4
5
use Oc\GeoCache\Enum\WaypointType;
6
use Oc\GeoCache\Exception\UnknownWaypointTypeException;
7
use Oc\Repository\Exception\RecordNotFoundException;
8
use Oc\Repository\Exception\RecordsNotFoundException;
9
10
class GeoCacheService
11
{
12
    /**
13
     * @var GeoCacheRepository
14
     */
15
    private $geoCacheRepository;
16
17
    /**
18
     * @param GeoCacheRepository $geoCacheRepository
19
     */
20
    public function __construct(GeoCacheRepository $geoCacheRepository)
21
    {
22
        $this->geoCacheRepository = $geoCacheRepository;
23
    }
24
25
    /**
26
     * Fetches all GeoCaches.
27
     *
28
     * @return GeoCacheEntity[]
29
     */
30
    public function fetchAll()
31
    {
32
        try {
33
            $result = $this->geoCacheRepository->fetchAll();
34
        } catch (RecordsNotFoundException $e) {
35
            $result = [];
36
        }
37
38
        return $result;
39
    }
40
41
    /**
42
     * Fetch a waypoint by given type.
43
     *
44
     * @param string $waypoint
45
     *
46
     * @return GeoCacheEntity|null
47
     */
48
    public function fetchByWaypoint($waypoint)
49
    {
50
        $waypointEntity = null;
51
52
        try {
53
            $waypointType = WaypointType::guess($waypoint);
54
55
            if ($waypointType === WaypointType::OC) {
56
                $waypointEntity = $this->geoCacheRepository->fetchOneBy([
57
                    'wp_oc' => $waypoint,
58
                ]);
59
            } elseif ($waypointType === WaypointType::GC) {
60
                $waypointEntity = $this->geoCacheRepository->fetchGCWaypoint($waypoint);
61
            }
62
        } catch (RecordNotFoundException $e) {
63
            $waypointEntity = null;
64
        } catch (UnknownWaypointTypeException $e) {
65
            $waypointEntity = null;
66
        }
67
68
        return $waypointEntity;
69
    }
70
71
    /**
72
     * Creates a GeoCache in the database.
73
     *
74
     * @param GeoCacheEntity $entity
75
     *
76
     * @return GeoCacheEntity
77
     */
78
    public function create(GeoCacheEntity $entity)
79
    {
80
        return $this->geoCacheRepository->create($entity);
81
    }
82
83
    /**
84
     * Update a GeoCache in the database.
85
     *
86
     * @param GeoCacheEntity $entity
87
     *
88
     * @return GeoCacheEntity
89
     */
90
    public function update(GeoCacheEntity $entity)
91
    {
92
        return $this->geoCacheRepository->update($entity);
93
    }
94
95
    /**
96
     * Removes a GeoCache from the database.
97
     *
98
     * @param GeoCacheEntity $entity
99
     *
100
     * @return GeoCacheEntity
101
     */
102
    public function remove(GeoCacheEntity $entity)
103
    {
104
        return $this->geoCacheRepository->remove($entity);
105
    }
106
}
107