Completed
Pull Request — master (#9)
by Rafael Gonçalves
03:18
created

GitDataCollector::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Kendrick\SymfonyDebugToolbarGit\DataCollector;
4
5
use Kendrick\SymfonyDebugToolbarGit\Git\Git;
6
use Kendrick\SymfonyDebugToolbarGit\Git\GitLastCommit;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Kendrick\SymfonyDebugToolbarGit\Git\GitCommand;
12
13
/**
14
 * Class GitDataCollector
15
 * @package Kendrick\SymfonyDebugToolbarGit\DataCollector
16
 */
17
class GitDataCollector extends DataCollector
18
{
19
    private $gitService;
20
21
    /**
22
     * @param $repositoryCommitUrl
23
     */
24
    public function __construct(ContainerInterface $container)
25
    {
26
        $this->gitService = $container->get('debug.toolbar.git');
27
        $this->data['repositoryCommitUrl'] = $container->getParameter('symfony_debug_toolbar_git.repository_commit_url');
28
        $this->data['gitData'] = true;
29
    }
30
31
    /**
32
     * Collect Git data for DebugBar (branch,commit,author,email,merge,date,message)
33
     *
34
     * @param Request $request
35
     * @param Response $response
36
     * @param \Exception $exception
37
     */
38
    public function collect(Request $request, Response $response, \Exception $exception = null)
39
    {
40
        if (!$this->gitService->GitDirExist()) {
41
42
            $this->data['gitData'] = false;
43
            $this->data['gitDir'] = $this->gitService->getGitDirInConfiguration();
44
45
            return;
46
        }
47
48
        $this->data['branch'] = $this->gitService->shellExec(GitCommand::GIT_CURRENT_BRANCH);
49
        $this->data['commit'] = $this->gitService->shellExec(GitCommand::GIT_HASH_LAST_COMMIT);
50
        $this->data['author'] = $this->gitService->shellExec(GitCommand::GIT_AUTHOR_LAST_COMMIT);
51
        $this->data['email'] = $this->gitService->shellExec(GitCommand::GIT_EMAIL_LAST_COMMIT);
52
        $this->data['message'] = $this->gitService->shellExec(GitCommand::GIT_MESSAGE_LAST_COMMIT);
53
        $this->data['merge'] = $this->gitService->shellExec(GitCommand::GIT_ABBREVIATED_PARENT_HASHES. $this->data['commit']);
54
        $this->getDateCommit();
55
    }
56
57
    /**
58
     * true if there is some data : used by the view
59
     *
60
     * @return string
61
     */
62
    public function getGitData()
63
    {
64
        return $this->getData('gitData');
65
    }
66
67
    /**
68
     * Actual branch name
69
     *
70
     * @return string
71
     */
72
    public function getBranch()
73
    {
74
        return $this->getData('branch');
75
    }
76
77
    /**
78
     * Commit ID
79
     *
80
     * @return string
81
     */
82
    public function getCommit()
83
    {
84
        return $this->getData('commit');
85
    }
86
87
    /**
88
     * Merge information
89
     *
90
     * @return string
91
     */
92
    public function getMerge()
93
    {
94
        return $this->getData('merge');
95
    }
96
97
    /**
98
     * Merge information
99
     *
100
     * @return string
101
     */
102
    public function getGitDIr()
103
    {
104
        return $this->getData('gitDir');
105
    }
106
107
    /**
108
     * Author
109
     *
110
     * @return string
111
     */
112
    public function getAuthor()
113
    {
114
        return $this->getData('author');
115
    }
116
117
    /**
118
     * Author's email
119
     *
120
     * @return string
121
     */
122
    public function getEmail()
123
    {
124
        return $this->getData('email');
125
    }
126
127
    /**
128
     * Commit date
129
     *
130
     * @return string
131
     */
132
    public function getDate()
133
    {
134
        return $this->getData('date');
135
    }
136
137
    /**
138
     * Minutes since last commit
139
     *
140
     * @return string
141
     */
142
    public function getTimeCommitIntervalMinutes()
143
    {
144
        return $this->getData('timeCommitIntervalMinutes');
145
    }
146
147
    /**
148
     * Seconds since latest commit
149
     *
150
     * @return string
151
     */
152
    public function getTimeCommitIntervalSeconds()
153
    {
154
        return $this->getData('timeCommitIntervalSeconds');
155
    }
156
157
    /**
158
     * Commit message
159
     *
160
     * @return string
161
     */
162
    public function getMessage()
163
    {
164
        return $this->getData('message');
165
    }
166
167
    /**
168
     * Commit URL
169
     *
170
     * @return string
171
     */
172
    public function getCommitUrl()
173
    {
174
        return $this->data['repositoryCommitUrl'];
175
    }
176
177
    /**
178
     * Checks and returns the data
179
     *
180
     * @param string $data
181
     * @return string
182
     */
183
    private function getData($data)
184
    {
185
        return (isset($this->data[$data])) ? $this->data[$data] : '';
186
    }
187
188
    /**
189
     * DataCollector name : used by service declaration into container.yml
190
     *
191
     * @return string
192
     */
193
    public function getName()
194
    {
195
        return 'datacollector_git';
196
    }
197
198
    /**
199
     * Change icons color according to the version of symfony
200
     *
201
     * #3f3f3f < 2.8
202
     * #AAAAAA >= 2.8
203
     *
204
     * @return string
205
     */
206
    public final function getIconColor()
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
207
    {
208
        if ((float)$this->getSymfonyVersion() >= 2.8) {
209
            return $this->data['iconColor'] = '#AAAAAA';
210
        }
211
212
        return $this->data['iconColor'] = '#3F3F3F';#3F3F3F
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    private function getSymfonyVersion()
219
    {
220
        $symfonyVersion = \Symfony\Component\HttpKernel\Kernel::VERSION;
221
        $symfonyVersion = explode('.', $symfonyVersion, -1);
222
        $symfonyMajorMinorVersion = implode('.', $symfonyVersion);
223
224
        return $symfonyMajorMinorVersion;
225
    }
226
227
    private function verifyExistMergeInCommit()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
228
    {
229
        $lastCommit = $this->gitService->exec(GitCommand::GIT_LOG_MINUS_ONE);
230
        if (strpos($lastCommit, 'Merge') === 0) {
231
            // merge information
232
            $this->data['merge'] = trim(substr($lastCommit, 6));
233
        }
234
    }
235
236
    private function getDateCommit()
237
    {
238
        $date = $this->gitService->shellExec(GitCommand::GIT_COMMIT_DATE);
239
        $dateCommit = date_create($date);
240
241
        // actual date at runtime
242
        $dateRuntime = new \DateTime();
243
        $dateNow = date_create($dateRuntime->format('Y-m-d H:i:s'));
244
        // difference
245
        $time = date_diff($dateCommit, $dateNow);
246
247
        // static time difference : minutes and seconds
248
        $this->data['timeCommitIntervalMinutes'] = $time->format('%y') * 365 * 24 * 60 + $time->format('%m') * 30 * 24 * 60 + $time->format('%d') * 24 * 60 + $time->format('%h') * 60 + $time->format('%i');
249
        // full readable date
250
        $this->data['date'] = $date;
251
    }
252
}
253
254