TypeProvider::getTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Cp\Provider;
4
5
use Cp\DomainObject\TypeInterface;
6
7
/**
8
 * Class Type
9
 */
10
class TypeProvider
11
{
12
    /**
13
     * @return array
14
     */
15 5
    public function getTypes()
16
    {
17
        return [
18 5
            '10' => TypeInterface::TYPE_10K,
19 5
            '21' => TypeInterface::TYPE_SEMI,
20 5
            '42' => TypeInterface::TYPE_MARATHON,
21 5
        ];
22
    }
23
24
    /**
25
     * @param string $type
26
     *
27
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
28
     * @throws \Exception
29
     */
30 2
    public function getTypeByName($type)
31
    {
32 2
        $key = array_search($type, $this->getTypes());
33
34 2
        if (false !== $key) {
35 1
            return $key;
36
        }
37
38 1
        throw new \Exception(sprintf(
39 1
            'Type: "%s" not found, allowed %s',
40 1
            $type,
41 1
            implode(', ', $this->getTypes())
42 1
        ));
43
    }
44
45
    /**
46
     * @param string $key
47
     *
48
     * @return string
49
     * @throws \Exception
50
     */
51 2
    public function getTypeByKey($key)
52
    {
53 2
        if (isset($this->getTypes()[$key])) {
54 1
            return $this->getTypes()[$key];
55
        }
56
57 1
        throw new \Exception(sprintf(
58 1
            'Key: "%s" not found, allowed %s',
59 1
            $key,
60 1
            implode(', ', array_keys($this->getTypes()))
61 1
        ));
62
    }
63
}
64