diff --git a/Puc/v4p2/Vcs/GitLabApi.php b/Puc/v4p2/Vcs/GitLabApi.php index a32792b..4e78f7c 100644 --- a/Puc/v4p2/Vcs/GitLabApi.php +++ b/Puc/v4p2/Vcs/GitLabApi.php @@ -1,6 +1,6 @@ repositoryHost = @parse_url( $repositoryUrl, PHP_URL_HOST ); + $this->repositoryHost = @parse_url($repositoryUrl, PHP_URL_HOST); // find the repository information - $path = @parse_url( $repositoryUrl, PHP_URL_PATH ); - if ( preg_match( '@^/?(?P[^/]+?)/(?P[^/#?&]+?)/?$@', $path, $matches ) ) { + $path = @parse_url($repositoryUrl, PHP_URL_PATH); + if ( preg_match('@^/?(?P[^/]+?)/(?P[^/#?&]+?)/?$@', $path, $matches) ) { $this->userName = $matches['username']; $this->repositoryName = $matches['repository']; } // this is not a traditional url, it could be gitlab is in a deeper subdirectory else { // get the path segments - $segments = explode( '/', untrailingslashit( ltrim( $path, '/' ) ) ); + $segments = explode('/', untrailingslashit(ltrim($path, '/'))); // we need atleast /user-name/repository-name/ - if ( sizeof ( $segments ) < 2 ) { - throw new InvalidArgumentException( 'Invalid GitLab repository URL: "' . $repositoryUrl . '"' ); + if ( sizeof($segments) < 2 ) { + throw new InvalidArgumentException('Invalid GitLab repository URL: "' . $repositoryUrl . '"'); } // get the username and repository name - $usernameRepo = array_splice( $segments, -2, 2 ); + $usernameRepo = array_splice($segments, -2, 2); $this->userName = $usernameRepo[0]; $this->repositoryName = $usernameRepo[1]; // append the remaining segments to the host - $this->repositoryHost = trailingslashit( $this->repositoryHost ) . implode( '/', $segments ); + $this->repositoryHost = trailingslashit($this->repositoryHost) . implode('/', $segments); } - parent::__construct( $repositoryUrl, $accessToken ); + parent::__construct($repositoryUrl, $accessToken); } /** @@ -75,23 +70,23 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @return Puc_v4p2_Vcs_Reference|null */ public function getLatestTag() { - $tags = $this->api( '/:user/:repo/repository/tags' ); - if ( is_wp_error( $tags ) || empty( $tags ) || ! is_array( $tags ) ) { + $tags = $this->api('/:user/:repo/repository/tags'); + if ( is_wp_error($tags) || empty($tags) || !is_array($tags) ) { return null; } - $versionTags = $this->sortTagsByVersion( $tags ); - if ( empty( $versionTags ) ) { + $versionTags = $this->sortTagsByVersion($tags); + if ( empty($versionTags) ) { return null; } $tag = $versionTags[0]; - return new Puc_v4p2_Vcs_Reference( array( + return new Puc_v4p2_Vcs_Reference(array( 'name' => $tag->name, - 'version' => ltrim( $tag->name, 'v' ), - 'downloadUrl' => $this->buildArchiveDownloadUrl( $tag->name ), + 'version' => ltrim($tag->name, 'v'), + 'downloadUrl' => $this->buildArchiveDownloadUrl($tag->name), 'apiResponse' => $tag - ) ); + )); } /** @@ -100,19 +95,19 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $branchName * @return null|Puc_v4p2_Vcs_Reference */ - public function getBranch( $branchName ) { - $branch = $this->api( '/:user/:repo/repository/branches/' . $branchName ); - if ( is_wp_error( $branch ) || empty( $branch ) ) { + public function getBranch($branchName) { + $branch = $this->api('/:user/:repo/repository/branches/' . $branchName); + if ( is_wp_error($branch) || empty($branch) ) { return null; } - $reference = new Puc_v4p2_Vcs_Reference( array( + $reference = new Puc_v4p2_Vcs_Reference(array( 'name' => $branch->name, - 'downloadUrl' => $this->buildArchiveDownloadUrl( $branch->name ), + 'downloadUrl' => $this->buildArchiveDownloadUrl($branch->name), 'apiResponse' => $branch, - ) ); + )); - if ( isset ( $branch->commit, $branch->commit->committed_date ) ) { + if ( isset($branch->commit, $branch->commit->committed_date) ) { $reference->updated = $branch->commit->committed_date; } @@ -125,9 +120,9 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $ref Reference name (e.g. branch or tag). * @return string|null */ - public function getLatestCommitTime( $ref ) { - $commits = $this->api( '/:user/:repo/repository/commits/', array( 'ref_name' => $ref ) ); - if ( is_wp_error( $commits ) || ! is_array( $commits ) || ! isset( $commits[0] ) ) { + public function getLatestCommitTime($ref) { + $commits = $this->api('/:user/:repo/repository/commits/', array('ref_name' => $ref)); + if ( is_wp_error($commits) || !is_array($commits) || !isset($commits[0]) ) { return null; } @@ -141,23 +136,23 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param array $queryParams * @return mixed|WP_Error */ - protected function api( $url, $queryParams = array() ) { - $url = $this->buildApiUrl( $url, $queryParams ); + protected function api($url, $queryParams = array()) { + $url = $this->buildApiUrl($url, $queryParams); - $options = array( 'timeout' => 10 ); - if ( ! empty( $this->httpFilterName ) ) { - $options = apply_filters( $this->httpFilterName, $options ); + $options = array('timeout' => 10); + if ( !empty($this->httpFilterName) ) { + $options = apply_filters($this->httpFilterName, $options); } - $response = wp_remote_get( $url, $options ); - if ( is_wp_error( $response ) ) { + $response = wp_remote_get($url, $options); + if ( is_wp_error($response) ) { return $response; } - $code = wp_remote_retrieve_response_code( $response ); - $body = wp_remote_retrieve_body( $response ); + $code = wp_remote_retrieve_response_code($response); + $body = wp_remote_retrieve_body($response); if ( $code === 200 ) { - return json_decode( $body ); + return json_decode($body); } return new WP_Error( @@ -173,25 +168,25 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param array $queryParams * @return string */ - protected function buildApiUrl( $url, $queryParams ) { + protected function buildApiUrl($url, $queryParams) { $variables = array( 'user' => $this->userName, 'repo' => $this->repositoryName ); - foreach ( $variables as $name => $value ) { - $url = str_replace( "/:{$name}", urlencode( '/' . $value ), $url ); + foreach ($variables as $name => $value) { + $url = str_replace("/:{$name}", urlencode('/' . $value), $url); } - $url = substr( $url, 3 ); - $url = sprintf( 'https://%1$s/api/v4/projects/%2$s', $this->repositoryHost, $url ); + $url = substr($url, 3); + $url = sprintf('https://%1$s/api/v4/projects/%2$s', $this->repositoryHost, $url); - if ( $this->accessToken ) { + if ( !empty($this->accessToken) ) { $queryParams['private_token'] = $this->accessToken; } - if ( ! empty( $queryParams ) ) { - $url = add_query_arg( $queryParams, $url ); + if ( !empty($queryParams) ) { + $url = add_query_arg($queryParams, $url); } return $url; @@ -204,13 +199,13 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $ref * @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error. */ - public function getRemoteFile( $path, $ref = 'master' ) { - $response = $this->api( '/:user/:repo/repository/files/' . $path, array( 'ref' => $ref ) ); - if ( is_wp_error( $response ) || ! isset( $response->content ) || $response->encoding !== 'base64' ) { + public function getRemoteFile($path, $ref = 'master') { + $response = $this->api('/:user/:repo/repository/files/' . $path, array('ref' => $ref)); + if ( is_wp_error($response) || !isset($response->content) || $response->encoding !== 'base64' ) { return null; } - return base64_decode( $response->content ); + return base64_decode($response->content); } /** @@ -219,16 +214,17 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $ref * @return string */ - public function buildArchiveDownloadUrl( $ref = 'master' ) { + public function buildArchiveDownloadUrl($ref = 'master') { $url = sprintf( 'https://%1$s/%2$s/%3$s/repository/%4$s/archive.zip', $this->repositoryHost, - urlencode( $this->userName ), - urlencode( $this->repositoryName ), - urlencode( $ref ) ); + urlencode($this->userName), + urlencode($this->repositoryName), + urlencode($ref) + ); - if ( $this->accessToken ) { - $url = add_query_arg( 'private_token', $this->accessToken, $url ); + if ( !empty($this->accessToken) ) { + $url = add_query_arg('private_token', $this->accessToken, $url); } return $url; @@ -240,8 +236,8 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $tagName * @return Puc_v4p2_Vcs_Reference|null */ - public function getTag( $tagName ) { - throw new LogicException( 'The ' . __METHOD__ . ' method is not implemented and should not be used.' ); + public function getTag($tagName) { + throw new LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.'); } /** @@ -250,7 +246,7 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : * @param string $configBranch Start looking in this branch. * @return null|Puc_v4p2_Vcs_Reference */ - public function chooseReference( $configBranch ) { + public function chooseReference($configBranch) { $updateSource = null; // GitLab doesn't handle releases the same as GitHub so just use the latest tag @@ -258,16 +254,16 @@ if ( ! class_exists( 'Puc_v4p2_Vcs_GitLabApi', false ) ) : $updateSource = $this->getLatestTag(); } - if ( empty( $updateSource ) ) { - $updateSource = $this->getBranch( $configBranch ); + if ( empty($updateSource) ) { + $updateSource = $this->getBranch($configBranch); } return $updateSource; } - public function setAuthentication( $credentials ) { - parent::setAuthentication( $credentials ); - $this->accessToken = is_string( $credentials ) ? $credentials : null; + public function setAuthentication($credentials) { + parent::setAuthentication($credentials); + $this->accessToken = is_string($credentials) ? $credentials : null; } }