1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Collector;
3:
4: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Remote;
5: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Commit;
6: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Git;
7: use Contrib\Component\System\Git\GitCommand;
8:
9: 10: 11: 12: 13:
14: class GitInfoCollector
15: {
16: 17: 18: 19: 20:
21: protected $command;
22:
23: 24: 25: 26: 27:
28: public function __construct(GitCommand $command)
29: {
30: $this->command = $command;
31: }
32:
33:
34:
35: 36: 37: 38: 39:
40: public function collect()
41: {
42: $branch = $this->collectBranch();
43: $commit = $this->collectCommit();
44: $remotes = $this->collectRemotes();
45:
46: return new Git($branch, $commit, $remotes);
47: }
48:
49:
50:
51: 52: 53: 54: 55: 56:
57: protected function collectBranch()
58: {
59: $branchesResult = $this->command->getBranches();
60:
61: foreach ($branchesResult as $result) {
62: if (strpos($result, '* ') === 0) {
63: $exploded = explode('* ', $result, 2);
64:
65: return $exploded[1];
66: }
67: }
68:
69: throw new \RuntimeException();
70: }
71:
72: 73: 74: 75: 76: 77:
78: protected function collectCommit()
79: {
80: $commitResult = $this->command->getHeadCommit();
81:
82: if (count($commitResult) !== 6 || array_keys($commitResult) !== range(0, 5)) {
83: throw new \RuntimeException();
84: }
85:
86: $commit = new Commit();
87:
88: return $commit
89: ->setId($commitResult[0])
90: ->setAuthorName($commitResult[1])
91: ->setAuthorEmail($commitResult[2])
92: ->setCommitterName($commitResult[3])
93: ->setCommitterEmail($commitResult[4])
94: ->setMessage($commitResult[5]);
95: }
96:
97: 98: 99: 100: 101: 102:
103: protected function collectRemotes()
104: {
105: $remotesResult = $this->command->getRemotes();
106:
107: if (count($remotesResult) === 0) {
108: throw new \RuntimeException();
109: }
110:
111:
112: $results = array();
113:
114: foreach ($remotesResult as $result) {
115: if (strpos($result, ' ') !== false) {
116: list($remote) = explode(' ', $result, 2);
117:
118: $results[] = $remote;
119: }
120: }
121:
122:
123: $results = array_unique($results);
124:
125:
126: $remotes = array();
127:
128: foreach ($results as $result) {
129: if (strpos($result, "\t") !== false) {
130: list($name, $url) = explode("\t", $result, 2);
131:
132: $remote = new Remote();
133: $remotes[] = $remote->setName($name)->setUrl($url);
134: }
135: }
136:
137: return $remotes;
138: }
139:
140:
141:
142: 143: 144: 145: 146:
147: public function getCommand()
148: {
149: return $this->command;
150: }
151: }
152: