1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Collector;
3:
4: use Contrib\Bundle\CoverallsV1Bundle\Config\Configuration;
5:
6: /**
7: * Environment variables collector for CI envrionment.
8: *
9: * @author Kitamura Satoshi <with.no.parachute@gmail.com>
10: */
11: class CiEnvVarsCollector
12: {
13: /**
14: * Configuration.
15: *
16: * @var Contrib\Bundle\CoverallsV1Bundle\Config\Configuration
17: */
18: protected $config;
19:
20: /**
21: * Environment variables.
22: *
23: * Overwritten through collection process.
24: *
25: * @var array
26: */
27: protected $env;
28:
29: /**
30: * Constructor.
31: *
32: * @param Configuration $config Configuration.
33: */
34: public function __construct(Configuration $config)
35: {
36: $this->config = $config;
37: }
38:
39: // API
40:
41: /**
42: * Collect environment variables.
43: *
44: * @param array $env $_SERVER environment.
45: * @return array
46: */
47: public function collect(array $env)
48: {
49: $this->env = $env;
50:
51: $this->fillTravisCi()
52: ->fillCircleCi()
53: ->fillJenkins()
54: ->fillLocal()
55: ->fillRepoToken();
56:
57: return $this->env;
58: }
59:
60: // internal method
61:
62: /**
63: * Fill Travis CI environment variables.
64: *
65: * "TRAVIS", "TRAVIS_JOB_ID" must be set.
66: *
67: * @return \Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector
68: */
69: protected function fillTravisCi()
70: {
71: if (isset($this->env['TRAVIS']) && $this->env['TRAVIS'] && isset($this->env['TRAVIS_JOB_ID'])) {
72: $this->env['CI_JOB_ID'] = $this->env['TRAVIS_JOB_ID'];
73:
74: if ($this->config->hasServiceName()) {
75: $this->env['CI_NAME'] = $this->config->getServiceName();
76: } else {
77: $this->env['CI_NAME'] = 'travis-ci';
78: }
79: }
80:
81: return $this;
82: }
83:
84: /**
85: * Fill CircleCI environment variables.
86: *
87: * "CIRCLECI", "CIRCLE_BUILD_NUM" must be set.
88: *
89: * @return \Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector
90: */
91: protected function fillCircleCi()
92: {
93: if (isset($this->env['CIRCLECI']) && $this->env['CIRCLECI'] && isset($this->env['CIRCLE_BUILD_NUM'])) {
94: $this->env['CI_BUILD_NUMBER'] = $this->env['CIRCLE_BUILD_NUM'];
95: $this->env['CI_NAME'] = 'circleci';
96: }
97:
98: return $this;
99: }
100:
101: /**
102: * Fill Jenkins environment variables.
103: *
104: * "JENKINS_URL", "BUILD_NUMBER" must be set.
105: *
106: * @return \Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector
107: */
108: protected function fillJenkins()
109: {
110: if (isset($this->env['JENKINS_URL']) && isset($this->env['BUILD_NUMBER'])) {
111: $this->env['CI_BUILD_NUMBER'] = $this->env['BUILD_NUMBER'];
112: $this->env['CI_BUILD_URL'] = $this->env['JENKINS_URL'];
113: $this->env['CI_NAME'] = 'jenkins';
114: }
115:
116: return $this;
117: }
118:
119: /**
120: * Fill local environment variables.
121: *
122: * "COVERALLS_RUN_LOCALLY" must be set.
123: *
124: * @return \Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector
125: */
126: protected function fillLocal()
127: {
128: if (isset($this->env['COVERALLS_RUN_LOCALLY']) && $this->env['COVERALLS_RUN_LOCALLY']) {
129: $this->env['CI_JOB_ID'] = null;
130: $this->env['CI_NAME'] = 'php-coveralls';
131: $this->env['COVERALLS_EVENT_TYPE'] = 'manual';
132: }
133:
134: return $this;
135: }
136:
137: /**
138: * Fill repo_token for unsupported CI service.
139: *
140: * "COVERALLS_REPO_TOKEN" must be set.
141: *
142: * @return \Contrib\Bundle\CoverallsV1Bundle\Collector\CiEnvVarsCollector
143: */
144: protected function fillRepoToken()
145: {
146: if ($this->config->hasRepoToken()) {
147: $this->env['COVERALLS_REPO_TOKEN'] = $this->config->getRepoToken();
148: }
149:
150: return $this;
151: }
152: }
153: