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