1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Api;
3:
4: use Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile;
5: use Contrib\Bundle\CoverallsV1Bundle\Collector\CloverXmlCoverageCollector;
6: use Contrib\Bundle\CoverallsV1Bundle\Collector\GitInfoCollector;
7: use Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector;
8: use Contrib\Component\System\Git\GitCommand;
9:
10: /**
11: * Jobs API.
12: *
13: * @author Kitamura Satoshi <with.no.parachute@gmail.com>
14: */
15: class Jobs extends CoverallsApi
16: {
17: /**
18: * URL for jobs API.
19: *
20: * @var string
21: */
22: const URL = 'https://coveralls.io/api/v1/jobs';
23:
24: /**
25: * Filename as a POST parameter.
26: *
27: * @var string
28: */
29: const FILENAME = 'json_file';
30:
31: /**
32: * JsonFile.
33: *
34: * @var Contrib\Bundle\CoverallsV1Bundle\Entity\JsonFile
35: */
36: protected $jsonFile;
37:
38: // API
39:
40: /**
41: * Collect clover XML into json_file.
42: *
43: * @return \Contrib\Bundle\CoverallsV1Bundle\Api\Jobs
44: */
45: public function collectCloverXml()
46: {
47: $srcDir = $this->config->getSrcDir();
48: $cloverXmlPaths = $this->config->getCloverXmlPaths();
49: $xmlCollector = new CloverXmlCoverageCollector();
50:
51: foreach ($cloverXmlPaths as $cloverXmlPath) {
52: $xml = simplexml_load_file($cloverXmlPath);
53:
54: $xmlCollector->collect($xml, $srcDir);
55: }
56:
57: $this->jsonFile = $xmlCollector->getJsonFile();
58:
59: if ($this->config->isExcludeNoStatements()) {
60: $this->jsonFile->excludeNoStatementsFiles();
61: }
62:
63: $this->jsonFile->sortSourceFiles();
64:
65: return $this;
66: }
67:
68: /**
69: * Collect git repository info into json_file.
70: *
71: * @return \Contrib\Bundle\CoverallsV1Bundle\Api\Jobs
72: */
73: public function collectGitInfo()
74: {
75: $command = new GitCommand();
76: $gitCollector = new GitInfoCollector($command);
77:
78: $this->jsonFile->setGit($gitCollector->collect());
79:
80: return $this;
81: }
82:
83: /**
84: * Collect environment variables.
85: *
86: * @param array $env $_SERVER environment.
87: * @return \Contrib\Bundle\CoverallsV1Bundle\Api\Jobs
88: */
89: public function collectEnvVars(array $env)
90: {
91: $envCollector = new CiEnvVarsCollector($this->config);
92:
93: $this->jsonFile->fillJobs($envCollector->collect($env));
94:
95: return $this;
96: }
97:
98: /**
99: * Dump uploading json file.
100: *
101: * @return \Contrib\Bundle\CoverallsV1Bundle\Api\Jobs
102: */
103: public function dumpJsonFile()
104: {
105: $jsonPath = $this->config->getJsonPath();
106:
107: file_put_contents($jsonPath, $this->jsonFile);
108:
109: return $this;
110: }
111:
112: /**
113: * Send json_file to jobs API.
114: *
115: * @return \Guzzle\Http\Message\Response|null
116: * @throws \RuntimeException
117: */
118: public function send()
119: {
120: if ($this->config->isDryRun()) {
121: return;
122: }
123:
124: $jsonPath = $this->config->getJsonPath();
125:
126: return $this->upload(static::URL, $jsonPath, static::FILENAME);
127: }
128:
129: // internal method
130:
131: /**
132: * Upload a file.
133: *
134: * @param string $url URL to upload.
135: * @param string $path File path.
136: * @param string $filename Filename.
137: * @return \Guzzle\Http\Message\Response Response.
138: * @throws \RuntimeException
139: */
140: protected function upload($url, $path, $filename)
141: {
142: $request = $this->client->post($url)->addPostFiles(array($filename => $path));
143:
144: return $request->send();
145: }
146:
147: // accessor
148:
149: /**
150: * Set JsonFile.
151: *
152: * @param JsonFile $jsonFile json_file content.
153: * @return \Contrib\Bundle\CoverallsV1Bundle\Api\Jobs
154: */
155: public function setJsonFile(JsonFile $jsonFile)
156: {
157: $this->jsonFile = $jsonFile;
158:
159: return $this;
160: }
161:
162: /**
163: * Return JsonFile.
164: *
165: * @return JsonFile
166: */
167: public function getJsonFile()
168: {
169: if (isset($this->jsonFile)) {
170: return $this->jsonFile;
171: }
172:
173: return null;
174: }
175: }
176: