Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

GeoCacheService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 10 2
A fetchByWaypoint() 0 20 4
A create() 0 4 1
A update() 0 4 1
A remove() 0 4 1
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
0 ignored issues
show
introduced by
GeoCacheRepository => \Array\GeoCacheRepository
Loading history...
13
     */
14
    private $geoCacheRepository;
15
16
    /**
17
     * @param GeoCacheRepository $geoCacheRepository
0 ignored issues
show
introduced by
GeoCacheRepository => \Array\GeoCacheRepository
Loading history...
18
     */
19
    public function __construct(GeoCacheRepository $geoCacheRepository)
20
    {
21
        $this->geoCacheRepository = $geoCacheRepository;
22
    }
23
24
    /**
25
     * Fetches all GeoCaches.
26
     *
27
     * @return GeoCacheEntity[]
0 ignored issues
show
introduced by
GeoCacheEntity[] => \Array\GeoCacheEntity[]
Loading history...
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
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
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
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
72
     *
73
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
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
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
84
     *
85
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
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
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
96
     *
97
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
98
     */
99
    public function remove(GeoCacheEntity $entity)
100
    {
101
        return $this->geoCacheRepository->remove($entity);
102
    }
103
}
104