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

GitDataCollector::getDateCommit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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