Completed
Push — master ( 69bbac...c6a45b )
by Dragos
03:11
created

AbstractSportMapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getMap() 0 1 ?
A sportFromCode() 0 7 1
A codeFromSport() 0 13 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace SportTrackerConnector\Core\Workout;
6
7
/**
8
 * Abstract class for tracker sport definitions.
9
 */
10
abstract class AbstractSportMapper implements SportMapperInterface
11
{
12
    /**
13
     * Get the map between the tracker's sport codes and internal sport codes.
14
     *
15
     * The key should be the internal sport code.
16
     *
17
     * @return array
18
     */
19
    abstract protected function getMap();
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function sportFromCode($code) : string
25
    {
26
        $code = strtolower($code);
27
        $codes = array_flip(static::getMap());
28
29
        return $codes[$code] ?? self::OTHER;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function codeFromSport($sport) : string
36
    {
37
        $sport = strtolower($sport);
38
        $codes = static::getMap();
39
40
        if (array_key_exists($sport, $codes)) {
41
            return $codes[$sport];
42
        } elseif (array_key_exists(self::OTHER, $codes)) {
43
            return $codes[self::OTHER];
44
        }
45
46
        return '';
47
    }
48
}
49