Passed
Pull Request — development (#682)
by Nick
07:18
created

GeoCacheService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 97
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
/**
10
 * Class GeoCacheService
11
 *
12
 * @package Oc\GeoCache\Persistence\GeoCache
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
13
 */
14
class GeoCacheService
15
{
16
    /**
17
     * @var GeoCacheRepository
0 ignored issues
show
introduced by
GeoCacheRepository => \Array\GeoCacheRepository
Loading history...
18
     */
19
    private $geoCacheRepository;
20
21
    /**
22
     * GeoCacheService constructor.
23
     *
24
     * @param GeoCacheRepository $geoCacheRepository
0 ignored issues
show
introduced by
GeoCacheRepository => \Array\GeoCacheRepository
Loading history...
25
     */
26
    public function __construct(GeoCacheRepository $geoCacheRepository)
27
    {
28
        $this->geoCacheRepository = $geoCacheRepository;
29
    }
30
31
    /**
32
     * Fetches all GeoCaches.
33
     *
34
     * @return GeoCacheEntity[]
0 ignored issues
show
introduced by
GeoCacheEntity[] => \Array\GeoCacheEntity[]
Loading history...
35
     */
36
    public function fetchAll()
37
    {
38
        try {
39
            $result = $this->geoCacheRepository->fetchAll();
40
        } catch (RecordsNotFoundException $e) {
41
            $result = [];
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * Fetch a waypoint by given type.
49
     *
50
     * @param string $waypoint
51
     *
52
     * @return GeoCacheEntity|null
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
53
     */
54
    public function fetchByWaypoint($waypoint)
55
    {
56
        $waypointType = WaypointType::guess($waypoint);
57
58
        $waypointEntity = null;
59
60
        try {
61
            if ($waypointType === WaypointType::OC) {
62
                $waypointEntity = $this->geoCacheRepository->fetchOneBy([
63
                    'wp_oc' => $waypoint
64
                ]);
65
            } else if ($waypointType === WaypointType::GC) {
66
                $waypointEntity = $this->geoCacheRepository->fetchGCWaypoint($waypoint);
67
            }
68
        } catch (RecordNotFoundException $e) {
69
            $waypointEntity = null;
70
        }
71
72
        return $waypointEntity;
73
    }
74
75
    /**
76
     * Creates a GeoCache in the database.
77
     *
78
     * @param GeoCacheEntity $entity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
79
     *
80
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
81
     */
82
    public function create(GeoCacheEntity $entity)
83
    {
84
        return $this->geoCacheRepository->create($entity);
85
    }
86
87
    /**
88
     * Update a GeoCache in the database.
89
     *
90
     * @param GeoCacheEntity $entity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
91
     *
92
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
93
     */
94
    public function update(GeoCacheEntity $entity)
95
    {
96
        return $this->geoCacheRepository->update($entity);
97
    }
98
99
    /**
100
     * Removes a GeoCache from the database.
101
     *
102
     * @param GeoCacheEntity $entity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
103
     *
104
     * @return GeoCacheEntity
0 ignored issues
show
introduced by
GeoCacheEntity => \Array\GeoCacheEntity
Loading history...
105
     */
106
    public function remove(GeoCacheEntity $entity)
107
    {
108
        return $this->geoCacheRepository->remove($entity);
109
    }
110
}
111