Completed
Branch v0.1.2 (1da595)
by Guillaume
02:21
created

Service::startTimer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 3
1
<?php
2
3
namespace AlfredTime;
4
5
use AlfredTime\ServiceApiCall;
6
7
abstract class Service
8
{
9
    /**
10
     * @var mixed
11
     */
12
    protected $serviceApiCall = null;
13
14
    /**
15
     * @param  $timerId
16
     * @return mixed
17
     */
18
    public function deleteTimer($timerId)
19
    {
20
        return $this->serviceApiCall->send(
21
            $this->methodForAction('delete'),
22
            $this->apiDeleteUrl($timerId)
23
        );
24
    }
25
26
    /**
27
     * @param  $descrition
28
     * @param  $projectId
29
     * @param  $tagData
30
     * @return mixed
31
     */
32
    public function startTimer($description, $projectId, $tagData)
33
    {
34
        $timerId = null;
35
36
        $item = $this->generateTimer($description, $projectId, $tagData);
37
38
        $data = $this->serviceApiCall->send(
39
            $this->methodForAction('start'),
40
            $this->apiStartUrl(),
41
            ['json' => $item],
42
            true
43
        );
44
45
        if (isset($data['id']) === true) {
46
            $timerId = $data['id'];
47
        }
48
49
        if (isset($data['data']['id']) === true) {
50
            $timerId = $data['data']['id'];
51
        }
52
53
        return $timerId;
54
    }
55
56
    /**
57
     * @param  $timerId
58
     * @return mixed
59
     */
60
    public function stopTimer($timerId)
61
    {
62
        return $this->serviceApiCall->send(
63
            $this->methodForAction('stop'),
64
            $this->apiStopUrl($timerId)
65
        );
66
    }
67
68
    /**
69
     * @param $baseUri
70
     * @param $apiToken
71
     */
72
    protected function __construct($baseUri, $credentials)
73
    {
74
        $this->serviceApiCall = new ServiceApiCall([
75
            'base_uri' => $baseUri,
76
            'headers'  => [
77
                'Authorization' => 'Basic ' . $credentials,
78
            ],
79
        ]);
80
    }
81
82
    /**
83
     * @param $timerId
84
     */
85
    abstract protected function apiDeleteUrl($timerId);
86
87
    /**
88
     * @param $timerId
89
     */
90
    abstract protected function apiStopUrl($timerId);
91
92
    abstract protected function getOnlineData();
93
94
    /**
95
     * @param $data
96
     */
97
    abstract protected function getProjects($data);
98
99
    abstract protected function getRecentTimers();
100
101
    /**
102
     * @param $data
103
     */
104
    abstract protected function getTags($data);
105
106
    /**
107
     * @param $action
108
     * @return mixed
109
     */
110
    protected function methodForAction($action)
111
    {
112
        if (isset($this->methods[$action]) === false) {
0 ignored issues
show
Bug introduced by
The property methods does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
113
            return;
114
        }
115
116
        return $this->methods[$action];
117
    }
118
}
119