| Total Lines | 114 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 68 | static function () { |
||
| 69 | $now = date('c'); |
||
| 70 | |||
| 71 | $defaultConfig = [ |
||
| 72 | 'version' => getReleaseGitRef(), |
||
| 73 | 'version_prefix' => null, |
||
| 74 | 'refs' => [], |
||
| 75 | 'ref' => null, |
||
| 76 | 'commits' => getGitCommitsRefs(), |
||
| 77 | 'url' => null, |
||
| 78 | 'date_released' => $now, |
||
| 79 | 'date_deploy_started' => $now, |
||
| 80 | 'date_deploy_finished' => $now, |
||
| 81 | 'sentry_server' => 'https://sentry.io', |
||
| 82 | 'previous_commit' => null, |
||
| 83 | 'environment' => get('symfony_env', 'prod'), |
||
| 84 | 'deploy_name' => null, |
||
| 85 | ]; |
||
| 86 | |||
| 87 | $config = array_merge($defaultConfig, (array) get('sentry')); |
||
| 88 | array_walk( |
||
| 89 | $config, |
||
| 90 | static function (&$value) use ($config) { |
||
| 91 | if (is_callable($value)) { |
||
| 92 | $value = $value($config); |
||
| 93 | } |
||
| 94 | }, |
||
| 95 | ); |
||
| 96 | |||
| 97 | if ( |
||
| 98 | !isset($config['organization'], $config['token'], $config['version']) |
||
| 99 | || (empty($config['projects']) || !is_array($config['projects'])) |
||
| 100 | ) { |
||
| 101 | throw new \RuntimeException( |
||
| 102 | <<<EXAMPLE |
||
| 103 | Required data missing. Please configure sentry: |
||
| 104 | set( |
||
| 105 | 'sentry', |
||
| 106 | [ |
||
| 107 | 'organization' => 'exampleorg', |
||
| 108 | 'projects' => [ |
||
| 109 | 'exampleproj', |
||
| 110 | 'exampleproje2' |
||
| 111 | ], |
||
| 112 | 'token' => 'd47828...', |
||
| 113 | ] |
||
| 114 | );" |
||
| 115 | EXAMPLE, |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | $releaseData = array_filter( |
||
| 120 | [ |
||
| 121 | 'version' => ($config['version_prefix'] ?? '') . $config['version'], |
||
| 122 | 'refs' => $config['refs'], |
||
| 123 | 'ref' => $config['ref'], |
||
| 124 | 'url' => $config['url'], |
||
| 125 | 'commits' => array_slice($config['commits'] ?? [], 0), // reset keys to serialize as array in json |
||
| 126 | 'dateReleased' => $config['date_released'], |
||
| 127 | 'projects' => $config['projects'], |
||
| 128 | 'previousCommit' => $config['previous_commit'], |
||
| 129 | ], |
||
| 130 | ); |
||
| 131 | |||
| 132 | $releasesApiUrl = $config['sentry_server'] . '/api/0/organizations/' . $config['organization'] . '/releases/'; |
||
| 133 | $response = Httpie::post( |
||
| 134 | $releasesApiUrl, |
||
| 135 | ) |
||
| 136 | ->setopt(CURLOPT_TIMEOUT, 10) |
||
| 137 | ->header('Authorization', sprintf('Bearer %s', $config['token'])) |
||
| 138 | ->jsonBody($releaseData) |
||
| 139 | ->getJson(); |
||
| 140 | |||
| 141 | if (!isset($response['version'], $response['projects'])) { |
||
| 142 | throw new \RuntimeException(sprintf('Unable to create a release: %s', print_r($response, true))); |
||
|
|
|||
| 143 | } |
||
| 144 | |||
| 145 | writeln( |
||
| 146 | sprintf( |
||
| 147 | '<info>Sentry:</info> Release of version <comment>%s</comment> ' . |
||
| 148 | 'for projects: <comment>%s</comment> created successfully.', |
||
| 149 | $response['version'], |
||
| 150 | implode(', ', array_column($response['projects'], 'slug')), |
||
| 151 | ), |
||
| 152 | ); |
||
| 153 | |||
| 154 | $deployData = array_filter( |
||
| 155 | [ |
||
| 156 | 'environment' => $config['environment'], |
||
| 157 | 'name' => $config['deploy_name'], |
||
| 158 | 'url' => $config['url'], |
||
| 159 | 'dateStarted' => $config['date_deploy_started'], |
||
| 160 | 'dateFinished' => $config['date_deploy_finished'], |
||
| 161 | ], |
||
| 162 | ); |
||
| 163 | |||
| 164 | $response = Httpie::post( |
||
| 165 | $releasesApiUrl . $response['version'] . '/deploys/', |
||
| 166 | ) |
||
| 167 | ->setopt(CURLOPT_TIMEOUT, 10) |
||
| 168 | ->header('Authorization', sprintf('Bearer %s', $config['token'])) |
||
| 169 | ->jsonBody($deployData) |
||
| 170 | ->getJson(); |
||
| 171 | |||
| 172 | if (!isset($response['id'], $response['environment'])) { |
||
| 173 | throw new \RuntimeException(sprintf('Unable to create a deployment: %s', print_r($response, true))); |
||
| 174 | } |
||
| 175 | |||
| 176 | writeln( |
||
| 177 | sprintf( |
||
| 178 | '<info>Sentry:</info> Deployment <comment>%s</comment> ' . |
||
| 179 | 'for environment <comment>%s</comment> created successfully', |
||
| 180 | $response['id'], |
||
| 181 | $response['environment'], |
||
| 182 | ), |
||
| 295 |