Completed
Push — master ( 169e5d...b608d5 )
by Guillaume
02:16
created

Config::runningServices()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace AlfredTime;
4
5
/**
6
 * Config
7
 */
8
class Config
9
{
10
    /**
11
     * @var mixed
12
     */
13
    private $config = [];
14
15
    /**
16
     * @var array
17
     */
18
    private $currentImplementation = [
19
        'start'        => ['toggl', 'harvest'],
20
        'stop'         => ['toggl', 'harvest'],
21
        'delete'       => ['toggl', 'harvest'],
22
        'get_projects' => ['toggl', 'harvest'],
23
        'get_tags'     => ['toggl', 'harvest'],
24
        'get_timers'   => ['toggl'],
25
        'sync_data'    => ['toggl', 'harvest'],
26
    ];
27
28
    /**
29
     * @var array
30
     */
31
    private $services = [
32
        'toggl',
33
        'harvest',
34
    ];
35
36
    /**
37
     * @param $filename
38
     */
39
    public function __construct($filename = null)
40
    {
41
        if ($filename !== null) {
42
            $this->load($filename);
43
        }
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function activatedServices()
50
    {
51
        $activatedServices = [];
52
53
        foreach ($this->services as $service) {
54
            if ($this->isServiceActive($service) === true) {
55
                array_push($activatedServices, $service);
56
            }
57
        }
58
59
        return $activatedServices;
60
    }
61
62
    public function generateDefaultConfigurationFile()
63
    {
64
        $this->config = [
65
            'timer'   => [
66
                'primary_service' => 'toggl',
67
                'is_running'      => false,
68
                'toggl_id'        => null,
69
                'harvest_id'      => null,
70
                'description'     => '',
71
            ],
72
            'toggl'   => [
73
                'is_active'          => true,
74
                'api_token'          => '',
75
            ],
76
            'harvest' => [
77
78
                'is_active'          => true,
79
                'domain'             => '',
80
                'api_token'          => '',
81
            ],
82
        ];
83
84
        $this->save();
85
    }
86
87
    /**
88
     * @param  $section
89
     * @param  null       $param
90
     * @return mixed
91
     */
92
    public function get($section = null, $param = null)
93
    {
94
        if ($section === null) {
95
            return $this->config;
96
        } elseif ($param === null) {
97
            return $this->config[$section];
98
        }
99
100
        return $this->config[$section][$param];
101
    }
102
103
    /**
104
     * @param  string  $feature
105
     * @return mixed
106
     */
107
    public function implementedServicesForFeature($feature = null)
108
    {
109
        $services = [];
110
111
        if (isset($this->currentImplementation[$feature]) === true) {
112
            $services = $this->currentImplementation[$feature];
113
        }
114
115
        return $services;
116
    }
117
118
    /**
119
     * @return boolean
120
     */
121
    public function isConfigured()
122
    {
123
        return $this->config !== null;
124
    }
125
126
    /**
127
     * @param  $service
128
     * @return mixed
129
     */
130
    public function isServiceActive($service)
131
    {
132
        return $this->get($service, 'is_active');
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    public function runningServices()
139
    {
140
        $services = [];
141
142
        foreach ($this->activatedServices() as $service) {
143
            if ($this->get('timer', $service . '_id') !== null) {
144
                array_push($services, $service);
145
            }
146
        }
147
148
        return $services;
149
    }
150
151
    /**
152
     * @param $section
153
     * @param $param
154
     * @param $value
155
     */
156
    public function update($section, $param, $value)
157
    {
158
        $this->config[$section][$param] = $value;
159
        $this->save();
160
    }
161
162
    /**
163
     * @return mixed
164
     */
165
    private function load($filename)
166
    {
167
        if (file_exists($filename)) {
168
            $this->config = json_decode(file_get_contents($filename), true);
169
        }
170
    }
171
172
    private function save()
173
    {
174
        $workflowDir = getenv('alfred_workflow_data');
175
        $configFile = $workflowDir . '/config.json';
176
177
        if (file_exists($workflowDir) === false) {
178
            mkdir($workflowDir);
179
        }
180
181
        file_put_contents($configFile, json_encode($this->config, JSON_PRETTY_PRINT));
182
    }
183
}
184