|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the bitbucket-api package. |
| 5 | + * |
| 6 | + * (c) Alexandru G. <alex@gentle.ro> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Bitbucket\API\Repositories; |
| 13 | + |
| 14 | +use Bitbucket\API\Api; |
| 15 | +use Buzz\Message\MessageInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Manage branch restrictions on a repository |
| 19 | + * |
| 20 | + * @author Alexandru G. <alex@gentle.ro> |
| 21 | + */ |
| 22 | +class BranchRestrictions extends Api |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Get the information associated with a repository's branch restrictions |
| 26 | + * |
| 27 | + * @access public |
| 28 | + * @param string $account The team or individual account owning the repository. |
| 29 | + * @param string $repo The repository identifier. |
| 30 | + * @return MessageInterface |
| 31 | + */ |
| 32 | + public function all($account, $repo) |
| 33 | + { |
| 34 | + return $this->getClient()->setApiVersion('2.0')->get( |
| 35 | + sprintf('repositories/%s/%s/branch-restrictions', $account, $repo) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates restrictions for the specified repository. |
| 41 | + * |
| 42 | + * @access public |
| 43 | + * @param string $account The team or individual account owning the repository. |
| 44 | + * @param string $repo The repository identifier. |
| 45 | + * @param array $params Additional parameters |
| 46 | + * @return MessageInterface |
| 47 | + * |
| 48 | + * @throws \InvalidArgumentException |
| 49 | + */ |
| 50 | + public function create($account, $repo, $params = array()) |
| 51 | + { |
| 52 | + // allow developer to directly specify params as json if (s)he wants. |
| 53 | + if (!empty($params) && is_string($params)) { |
| 54 | + $params = $this->decodeJSON($params); |
| 55 | + } |
| 56 | + |
| 57 | + if (!empty($params) && is_array($params)) { |
| 58 | + $params = array_merge( |
| 59 | + array( |
| 60 | + 'kind' => 'push' |
| 61 | + ), |
| 62 | + $params |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (empty($params['kind']) or !in_array($params['kind'], array('push', 'delete', 'force'))) { |
| 67 | + throw new \InvalidArgumentException('Invalid restriction kind.'); |
| 68 | + } |
| 69 | + |
| 70 | + return $this->getClient()->setApiVersion('2.0')->post( |
| 71 | + sprintf('repositories/%s/%s/branch-restrictions', $account, $repo), |
| 72 | + json_encode($params), |
| 73 | + array('Content-Type' => 'application/json') |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get a specific restriction |
| 79 | + * |
| 80 | + * @access public |
| 81 | + * @param string $account The team or individual account owning the repository. |
| 82 | + * @param string $repo The repository identifier. |
| 83 | + * @param int $id The restriction's identifier. |
| 84 | + * @return MessageInterface |
| 85 | + */ |
| 86 | + public function get($account, $repo, $id) |
| 87 | + { |
| 88 | + return $this->getClient()->setApiVersion('2.0')->get( |
| 89 | + sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id) |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Updates a specific branch restriction. |
| 95 | + * |
| 96 | + * @access public |
| 97 | + * @param string $account The team or individual account owning the repository. |
| 98 | + * @param string $repo The repository identifier. |
| 99 | + * @param int $id The restriction's identifier. |
| 100 | + * @param array $params Additional parameters |
| 101 | + * @return MessageInterface |
| 102 | + * |
| 103 | + * @throws \InvalidArgumentException |
| 104 | + */ |
| 105 | + public function update($account, $repo, $id, $params = array()) |
| 106 | + { |
| 107 | + // allow developer to directly specify params as json if (s)he wants. |
| 108 | + if (!empty($params) && is_string($params)) { |
| 109 | + $params = $this->decodeJSON($params); |
| 110 | + } |
| 111 | + |
| 112 | + if (!empty($params['kind'])) { |
| 113 | + throw new \InvalidArgumentException('You cannot change the "kind" value.'); |
| 114 | + } |
| 115 | + |
| 116 | + return $this->getClient()->setApiVersion('2.0')->put( |
| 117 | + sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id), |
| 118 | + json_encode($params), |
| 119 | + array('Content-Type' => 'application/json') |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Delete a specific branch restriction. |
| 125 | + * |
| 126 | + * @access public |
| 127 | + * @param string $account The team or individual account owning the repository. |
| 128 | + * @param string $repo The repository identifier. |
| 129 | + * @param int $id The restriction's identifier. |
| 130 | + * @return MessageInterface |
| 131 | + * |
| 132 | + * @throws \InvalidArgumentException |
| 133 | + */ |
| 134 | + public function delete($account, $repo, $id) |
| 135 | + { |
| 136 | + return $this->getClient()->setApiVersion('2.0')->delete( |
| 137 | + sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id) |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments