├── .php_cs ├── LICENSE ├── composer.json └── src ├── CachetClient.php ├── Components ├── ComponentActions.php └── ComponentFactory.php ├── Exceptions ├── ClientException.php ├── ConnectException.php ├── Exception.php ├── ExceptionInterface.php ├── InvalidResponseException.php ├── ServerException.php └── TooManyRedirectsException.php ├── General ├── GeneralActions.php └── GeneralFactory.php ├── Groups ├── GroupActions.php └── GroupFactory.php ├── Incidents ├── IncidentActions.php └── IncidentFactory.php ├── Metrics ├── MetricActions.php └── MetricFactory.php ├── Points ├── PointActions.php └── PointFactory.php └── Subscribers ├── SubscriberActions.php └── SubscriberFactory.php /.php_cs: -------------------------------------------------------------------------------- 1 | finder(DefaultFinder::create()->in(__DIR__)) 75 | ->fixers($fixers) 76 | ->level(FixerInterface::NONE_LEVEL) 77 | ->setUsingCache(true); 78 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Damiano Petrungaro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "damianopetrungaro/cachet-sdk", 3 | "type": "library", 4 | "description": "A PHP SDK for Cachet, providing a full functionality access.", 5 | "require": { 6 | "guzzlehttp/guzzle": "^6.2" 7 | }, 8 | "require-dev": { 9 | "phpunit/phpunit": "@stable" 10 | }, 11 | "license": "MIT", 12 | "homepage": "https://github.com/damianopetrungaro/CachetSDK", 13 | "authors": [ 14 | { 15 | "name": "Damiano Petrungaro", 16 | "email": "damianopetrungaro@gmail.com" 17 | } 18 | ], 19 | "autoload": { 20 | "psr-4": { 21 | "Damianopetrungaro\\CachetSDK\\": "src/" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "Damianopetrungaro\\CachetSDKTest\\": "tests/Unit" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/CachetClient.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK; 9 | 10 | use GuzzleHttp\Client; 11 | use GuzzleHttp\Psr7\Request; 12 | use GuzzleHttp\Exception\ServerException; 13 | use GuzzleHttp\Exception\ClientException; 14 | use GuzzleHttp\Exception\ConnectException; 15 | use GuzzleHttp\Exception\TooManyRedirectsException; 16 | use Damianopetrungaro\CachetSDK\Exceptions\ClientException as CachetClientException; 17 | use Damianopetrungaro\CachetSDK\Exceptions\ServerException as CachetServerException; 18 | use Damianopetrungaro\CachetSDK\Exceptions\ConnectException as CachetConnectException; 19 | use Damianopetrungaro\CachetSDK\Exceptions\InvalidResponseException as CachetInvalidResponseException; 20 | use Damianopetrungaro\CachetSDK\Exceptions\TooManyRedirectsException as CachetTooManyRedirectsException; 21 | 22 | /** 23 | * Class CachetClient. 24 | * 25 | * Is a Guzzle Client for manage the API interaction from your application to cachet. 26 | * 27 | * @author Damiano Petrungaro 28 | */ 29 | class CachetClient 30 | { 31 | protected $client; 32 | 33 | /** 34 | * Cachet Client Singleton. 35 | * 36 | * @param string $endpoint 37 | * @param string $token 38 | * @param array $clientConfig 39 | */ 40 | public function __construct($endpoint, $token, array $clientConfig = []) 41 | { 42 | $basicConfig = [ 43 | 'base_uri' => $endpoint, 44 | 'headers' => [ 45 | 'Content-type' => 'application/json', 46 | 'X-Cachet-Token' => $token, 47 | ], 48 | ]; 49 | 50 | $config = array_merge($basicConfig, $clientConfig); 51 | 52 | $this->client = new Client($config); 53 | } 54 | 55 | public function call($method, $endpoint, array $params = []) 56 | { 57 | try { 58 | $request = new Request($method, $endpoint, ['http_errors' => false]); 59 | $response = $this->client->send($request, $params); 60 | $body = json_decode($response->getBody(), true); 61 | } catch (ConnectException $e) { 62 | throw new CachetConnectException($e->getRequest(), $e->getMessage(), $e->getPrevious()); 63 | } catch (ServerException $e) { 64 | throw new CachetServerException($e->getRequest(), $e->getMessage(), $e->getPrevious(), $e->getResponse()); 65 | } catch (ClientException $e) { 66 | throw new CachetClientException($e->getRequest(), $e->getMessage(), $e->getPrevious(), $e->getResponse()); 67 | } catch (TooManyRedirectsException $e) { 68 | throw new CachetTooManyRedirectsException($e->getRequest(), $e->getMessage(), $e->getPrevious(), $e->getResponse()); 69 | } 70 | 71 | if ((is_array($body) && ! array_key_exists('data', $body)) && ! empty($body)) { 72 | throw new CachetInvalidResponseException($request, $response); 73 | } 74 | 75 | return $body; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Components/ComponentActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Components; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class ComponentActions. 14 | * 15 | * The ComponentActions contains all the action for Components API available in CachetHQ 16 | */ 17 | class ComponentActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Components. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * ComponentActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Components for performance improvement. 62 | * 63 | * @param int $num 64 | * @param int $page 65 | * 66 | * @return array|bool 67 | */ 68 | public function cacheComponents($num = 1000, $page = 1) 69 | { 70 | if ($this->cached != null) { 71 | return $this->cached; 72 | } 73 | 74 | $this->cached = $this->client->call( 75 | 'GET', 76 | 'components', 77 | [ 78 | 'query' => [ 79 | 'per_page' => $num, 80 | 'current_page' => $page, 81 | ], 82 | ] 83 | ); 84 | 85 | return $this->cached; 86 | } 87 | 88 | /** 89 | * Get a defined number of Components. 90 | * 91 | * @param int $num 92 | * @param int $page 93 | * 94 | * @return array|bool 95 | */ 96 | public function indexComponents($num = 1000, $page = 1) 97 | { 98 | if ($this->cache === true) { 99 | return $this->cacheComponents($num, $page); 100 | } 101 | 102 | return $this->client->call( 103 | 'GET', 104 | 'components', 105 | [ 106 | 'query' => [ 107 | 'per_page' => $num, 108 | 'current_page' => $page, 109 | ], 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Get a specific Component. 116 | * 117 | * @param int $componentId 118 | * 119 | * @return bool 120 | */ 121 | public function getComponent($componentId) 122 | { 123 | return $this->client->call('GET', "components/$componentId"); 124 | } 125 | 126 | /** 127 | * Search if a defined number of Components exists. 128 | * 129 | * @param string $search 130 | * @param string $by 131 | * @param int $num 132 | * @param int $page 133 | * @param int $limit 134 | * 135 | * @return mixed 136 | */ 137 | public function searchComponents($search, $by, $limit = 1, $num = 1000, $page = 1) 138 | { 139 | $components = $this->indexComponents($num, $page)['data']; 140 | 141 | $filtered = array_filter( 142 | $components, 143 | function ($component) use ($search, $by) { 144 | 145 | if (array_key_exists($by, $component)) { 146 | if (strpos($component[$by], $search) !== false) { 147 | return $component; 148 | } 149 | if ($component[$by] === $search) { 150 | return $component; 151 | } 152 | } 153 | 154 | return false; 155 | } 156 | ); 157 | 158 | if ($limit == 1) { 159 | return array_shift($filtered); 160 | } 161 | 162 | return array_slice($filtered, 0, $limit); 163 | } 164 | 165 | /** 166 | * Store a Component. 167 | * 168 | * @param array $component 169 | * 170 | * @return bool 171 | */ 172 | public function storeComponent(array $component) 173 | { 174 | return $this->client->call('POST', 'components', ['json' => $component]); 175 | } 176 | 177 | /** 178 | * Update a specific Component. 179 | * 180 | * @param int $componentId 181 | * @param array $component 182 | * 183 | * @return bool 184 | */ 185 | public function updateComponent($componentId, array $component) 186 | { 187 | return $this->client->call('PUT', "components/$componentId", ['json' => $component]); 188 | } 189 | 190 | /** 191 | * Delete a specific Component. 192 | * 193 | * @param int $componentId 194 | * 195 | * @return bool 196 | */ 197 | public function deleteComponent($componentId) 198 | { 199 | return $this->client->call('DELETE', "components/$componentId"); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/Components/ComponentFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Components; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class ComponentFactory. 14 | * 15 | * The ComponentFactory generate a singleton for interact with Components in cachet 16 | */ 17 | class ComponentFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object ComponentActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new ComponentActions($client); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Exceptions/ClientException.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | use Psr\Http\Message\RequestInterface; 11 | use Psr\Http\Message\ResponseInterface; 12 | 13 | class ClientException extends Exception 14 | { 15 | public function __construct(RequestInterface $request, $message, \Exception $previous = null, ResponseInterface $response) 16 | { 17 | parent::__construct($request, $message, $previous, $response); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/ConnectException.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | use Psr\Http\Message\RequestInterface; 11 | 12 | class ConnectException extends Exception 13 | { 14 | public function __construct(RequestInterface $request, $message, \Exception $previous = null) 15 | { 16 | parent::__construct($request, $message, $previous); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | use RuntimeException; 11 | use Psr\Http\Message\RequestInterface; 12 | use Psr\Http\Message\ResponseInterface; 13 | 14 | class Exception extends RuntimeException implements ExceptionInterface 15 | { 16 | private $request; 17 | private $response; 18 | 19 | public function __construct(RequestInterface $request, $message, \Exception $previous = null, ResponseInterface $response = null) 20 | { 21 | parent::__construct($message, 0, $previous); 22 | $this->request = $request; 23 | $this->response = $response; 24 | } 25 | 26 | public function request() 27 | { 28 | return $this->request; 29 | } 30 | 31 | public function response() 32 | { 33 | return $this->response; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Exceptions/ExceptionInterface.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | interface ExceptionInterface 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidResponseException.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | use Psr\Http\Message\RequestInterface; 11 | use Psr\Http\Message\ResponseInterface; 12 | 13 | class InvalidResponseException extends Exception 14 | { 15 | public function __construct(RequestInterface $request, ResponseInterface $response, \Exception $previous = null) 16 | { 17 | parent::__construct($request, 'The data format received is invalid. "data" key is missing from the response array', $previous, $response); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/ServerException.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | class ServerException extends Exception 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /src/Exceptions/TooManyRedirectsException.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Exceptions; 9 | 10 | class TooManyRedirectsException extends Exception 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /src/General/GeneralActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\General; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | class GeneralActions 13 | { 14 | /** 15 | * Contains Cachet Client (using Guzzle). 16 | * 17 | * @var \GuzzleHttp\Client 18 | */ 19 | protected $client; 20 | 21 | /** 22 | * GroupActions constructor. 23 | * 24 | * @param CachetClient $client 25 | */ 26 | public function __construct(CachetClient $client) 27 | { 28 | $this->client = $client; 29 | } 30 | 31 | public function ping() 32 | { 33 | return $this->client->call('GET', 'ping'); 34 | } 35 | 36 | public function version() 37 | { 38 | return $this->client->call('GET', 'version'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/General/GeneralFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\General; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | class GeneralFactory 13 | { 14 | /** 15 | * Build an object. 16 | * 17 | * @param $client 18 | * 19 | * @return object GeneralActions 20 | */ 21 | public static function build(CachetClient $client) 22 | { 23 | return new GeneralActions($client); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Groups/GroupActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Groups; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class GroupActions. 14 | * 15 | * The GroupActions contains all the action for Groups API available in CachetHQ 16 | */ 17 | class GroupActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Groups. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * GroupActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Group for performance improvement. 62 | * 63 | * @param int $num 64 | * @param int $page 65 | * 66 | * @return array|bool 67 | */ 68 | public function cacheGroups($num = 1000, $page = 1) 69 | { 70 | if ($this->cached != null) { 71 | return $this->cached; 72 | } 73 | 74 | $this->cached = $this->client->call( 75 | 'GET', 76 | 'components/groups', 77 | [ 78 | 'query' => [ 79 | 'per_page' => $num, 80 | 'current_page' => $page, 81 | ], 82 | ] 83 | ); 84 | 85 | return $this->cached; 86 | } 87 | 88 | /** 89 | * Get a defined number of Groups. 90 | * 91 | * @param int $num 92 | * @param int $page 93 | * 94 | * @return array|bool 95 | */ 96 | public function indexGroups($num = 1000, $page = 1) 97 | { 98 | if ($this->cache != false) { 99 | return $this->cacheGroups($num, $page); 100 | } 101 | 102 | return $this->client->call( 103 | 'GET', 104 | 'components/groups', 105 | [ 106 | 'query' => [ 107 | 'per_page' => $num, 108 | 'current_page' => $page, 109 | ], 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Get a specific Group. 116 | * 117 | * @param int $groupId 118 | * 119 | * @return bool 120 | */ 121 | public function getGroup($groupId) 122 | { 123 | return $this->client->call('GET', "components/groups/$groupId"); 124 | } 125 | 126 | /** 127 | * Search if a defined number of Groups exists. 128 | * 129 | * @param string $search 130 | * @param string $by 131 | * @param int $num 132 | * @param int $page 133 | * @param int $limit 134 | * 135 | * @return mixed 136 | */ 137 | public function searchGroups($search, $by, $limit = 1, $num = 1000, $page = 1) 138 | { 139 | $groups = $this->indexGroups($num, $page)['data']; 140 | 141 | $filtered = array_filter( 142 | $groups, 143 | function ($group) use ($search, $by) { 144 | 145 | if (array_key_exists($by, $group)) { 146 | if (strpos($group[$by], $search) !== false) { 147 | return $group; 148 | } 149 | if ($group[$by] === $search) { 150 | return $group; 151 | } 152 | } 153 | 154 | return false; 155 | } 156 | ); 157 | 158 | if ($limit == 1) { 159 | return array_shift($filtered); 160 | } 161 | 162 | return array_slice($filtered, 0, $limit); 163 | } 164 | 165 | /** 166 | * Store a Group. 167 | * 168 | * @param array $group 169 | * 170 | * @return bool 171 | */ 172 | public function storeGroup(array $group) 173 | { 174 | return $this->client->call('POST', 'components/groups', ['json' => $group]); 175 | } 176 | 177 | /** 178 | * Update a specific Group. 179 | * 180 | * @param int $groupId 181 | * @param array $group 182 | * 183 | * @return bool 184 | */ 185 | public function updateGroup($groupId, array $group) 186 | { 187 | return $this->client->call('PUT', "components/groups/$groupId", ['json' => $group]); 188 | } 189 | 190 | /** 191 | * Delete a specific Group. 192 | * 193 | * @param int $groupId 194 | * 195 | * @return bool 196 | */ 197 | public function deleteGroup($groupId) 198 | { 199 | return $this->client->call('DELETE', "components/groups/$groupId"); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/Groups/GroupFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Groups; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class GroupFactory. 14 | * 15 | * The GroupFactory generate a singleton for interact with Groups in cachet 16 | */ 17 | class GroupFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object GroupActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new GroupActions($client); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Incidents/IncidentActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Incidents; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class IncidentActions. 14 | * 15 | * The IncidentActions contains all the action for Incidents API available in CachetHQ 16 | */ 17 | class IncidentActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Incidents. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * IncidentActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Incidents for performance improvement. 62 | * 63 | * @param int $num 64 | * @param int $page 65 | * 66 | * @return array|bool 67 | */ 68 | public function cacheIncidents($num = 1000, $page = 1) 69 | { 70 | if ($this->cached != null) { 71 | return $this->cached; 72 | } 73 | 74 | $this->cached = $this->client->call( 75 | 'GET', 76 | 'incidents', 77 | [ 78 | 'query' => [ 79 | 'per_page' => $num, 80 | 'current_page' => $page, 81 | ], 82 | ] 83 | ); 84 | 85 | return $this->cached; 86 | } 87 | 88 | /** 89 | * Get a defined number of Incidents. 90 | * 91 | * @param int $num 92 | * @param int $page 93 | * 94 | * @return array|bool 95 | */ 96 | public function indexIncidents($num = 1000, $page = 1) 97 | { 98 | if ($this->cache != false) { 99 | return $this->cacheIncidents($num, $page); 100 | } 101 | 102 | return $this->client->call( 103 | 'GET', 104 | 'incidents', 105 | [ 106 | 'query' => [ 107 | 'per_page' => $num, 108 | 'current_page' => $page, 109 | ], 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Get a specific Incident. 116 | * 117 | * @param int $incidentId 118 | * 119 | * @return bool 120 | */ 121 | public function getIncident($incidentId) 122 | { 123 | return $this->client->call('GET', "incidents/$incidentId"); 124 | } 125 | 126 | /** 127 | * Search if a defined number of Incidents exists. 128 | * 129 | * @param string $search 130 | * @param string $by 131 | * @param int $num 132 | * @param int $page 133 | * @param int $limit 134 | * 135 | * @return mixed 136 | */ 137 | public function searchIncidents($search, $by, $limit = 1, $num = 1000, $page = 1) 138 | { 139 | $incidents = $this->indexIncidents($num, $page)['data']; 140 | 141 | $filtered = array_filter( 142 | $incidents, 143 | function ($incident) use ($search, $by) { 144 | 145 | if (array_key_exists($by, $incident)) { 146 | if (strpos($incident[$by], $search) !== false) { 147 | return $incident; 148 | } 149 | if ($incident[$by] === $search) { 150 | return $incident; 151 | } 152 | } 153 | 154 | return false; 155 | } 156 | ); 157 | 158 | if ($limit == 1) { 159 | return array_shift($filtered); 160 | } 161 | 162 | return array_slice($filtered, 0, $limit); 163 | } 164 | 165 | /** 166 | * Store a Incident. 167 | * 168 | * @param array $incident 169 | * 170 | * @return bool 171 | */ 172 | public function storeIncident(array $incident) 173 | { 174 | return $this->client->call('POST', 'incidents', ['json' => $incident]); 175 | } 176 | 177 | /** 178 | * Update a specific Incident. 179 | * 180 | * @param int $incidentId 181 | * @param array $incident 182 | * 183 | * @return bool 184 | */ 185 | public function updateIncident($incidentId, array $incident) 186 | { 187 | return $this->client->call('PUT', "incidents/$incidentId", ['json' => $incident]); 188 | } 189 | 190 | /** 191 | * Delete a specific Incident. 192 | * 193 | * @param int $incidentId 194 | * 195 | * @return bool 196 | */ 197 | public function deleteIncident($incidentId) 198 | { 199 | return $this->client->call('DELETE', "incidents/$incidentId"); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/Incidents/IncidentFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Incidents; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class IncidentFactory. 14 | * 15 | * The IncidentFactory generate a singleton for interact with Incidents in cachet 16 | */ 17 | class IncidentFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object IncidentActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new IncidentActions($client); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Metrics/MetricActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Metrics; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class MetricActions. 14 | * 15 | * The MetricActions contains all the action for Metrics API available in CachetHQ 16 | */ 17 | class MetricActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Metrics. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * MetricActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Metrics for performance improvement. 62 | * 63 | * @param int $num 64 | * @param int $page 65 | * 66 | * @return array|bool 67 | */ 68 | public function cacheMetrics($num = 1000, $page = 1) 69 | { 70 | if ($this->cached != null) { 71 | return $this->cached; 72 | } 73 | 74 | $this->cached = $this->client->call( 75 | 'GET', 76 | 'metrics', 77 | [ 78 | 'query' => [ 79 | 'per_page' => $num, 80 | 'current_page' => $page, 81 | ], 82 | ] 83 | ); 84 | 85 | return $this->cached; 86 | } 87 | 88 | /** 89 | * Get a defined number of Metrics. 90 | * 91 | * @param int $num 92 | * @param int $page 93 | * 94 | * @return array|bool 95 | */ 96 | public function indexMetrics($num = 1000, $page = 1) 97 | { 98 | if ($this->cache != false) { 99 | return $this->cacheMetrics($num, $page); 100 | } 101 | 102 | return $this->client->call( 103 | 'GET', 104 | 'metrics', 105 | [ 106 | 'query' => [ 107 | 'per_page' => $num, 108 | 'current_page' => $page, 109 | ], 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Get a specific Metric. 116 | * 117 | * @param int $metricId 118 | * 119 | * @return bool 120 | */ 121 | public function getMetric($metricId) 122 | { 123 | return $this->client->call('GET', "metrics/$metricId"); 124 | } 125 | 126 | /** 127 | * Search if a defined number of Metrics exists. 128 | * 129 | * @param string $search 130 | * @param string $by 131 | * @param int $num 132 | * @param int $page 133 | * @param int $limit 134 | * 135 | * @return mixed 136 | */ 137 | public function searchMetrics($search, $by, $limit = 1, $num = 1000, $page = 1) 138 | { 139 | $metrics = $this->indexMetrics($num, $page)['data']; 140 | 141 | $filtered = array_filter( 142 | $metrics, 143 | function ($metric) use ($search, $by) { 144 | 145 | if (array_key_exists($by, $metric)) { 146 | if (strpos($metric[$by], $search) !== false) { 147 | return $metric; 148 | } 149 | if ($metric[$by] === $search) { 150 | return $metric; 151 | } 152 | } 153 | 154 | return false; 155 | } 156 | ); 157 | 158 | if ($limit == 1) { 159 | return array_shift($filtered); 160 | } 161 | 162 | return array_slice($filtered, 0, $limit); 163 | } 164 | 165 | /** 166 | * Store a Metric. 167 | * 168 | * @param array $metric 169 | * 170 | * @return bool 171 | */ 172 | public function storeMetric(array $metric) 173 | { 174 | return $this->client->call('POST', 'metrics', ['json' => $metric]); 175 | } 176 | 177 | /** 178 | * Update a specific Metric. 179 | * 180 | * @param int $metricId 181 | * @param array $metric 182 | * 183 | * @return bool 184 | */ 185 | public function updateMetric($metricId, array $metric) 186 | { 187 | return $this->client->call('PUT', "metrics/$metricId", ['json' => $metric]); 188 | } 189 | 190 | /** 191 | * Delete a specific Metric. 192 | * 193 | * @param int $metricId 194 | * 195 | * @return bool 196 | */ 197 | public function deleteMetric($metricId) 198 | { 199 | return $this->client->call('DELETE', "metrics/$metricId"); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/Metrics/MetricFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Metrics; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class MetricFactory. 14 | * 15 | * The MetricFactory generate a singleton for interact with Metrics in cachet 16 | */ 17 | class MetricFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object MetricActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new MetricActions($client); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Points/PointActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Points; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class PointActions. 14 | * 15 | * The PointActions contains all the action for Points API available in CachetHQ 16 | */ 17 | class PointActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Points. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * PointActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Points for performance improvement. 62 | * 63 | * @param int $metricId 64 | * @param int $num 65 | * @param int $page 66 | * 67 | * @return array|bool 68 | */ 69 | public function cachePoints($metricId, $num = 1000, $page = 1) 70 | { 71 | // If cache already exists return it 72 | if ($this->cached != null) { 73 | return $this->cached; 74 | } 75 | 76 | $this->cached = $this->client->call( 77 | 'GET', 78 | "metrics/$metricId/points", 79 | [ 80 | 'query' => [ 81 | 'per_page' => $num, 82 | 'current_page' => $page, 83 | ], 84 | ] 85 | ); 86 | 87 | return $this->cached; 88 | } 89 | 90 | /** 91 | * Get a defined number of Points. 92 | * 93 | * @param int $metricId 94 | * @param int $num 95 | * @param int $page 96 | * 97 | * @return array|bool 98 | */ 99 | public function indexPoints($metricId, $num = 1000, $page = 1) 100 | { 101 | if ($this->cache != false) { 102 | return $this->cachePoints($metricId, $num, $page); 103 | } 104 | 105 | return $this->client->call( 106 | 'GET', 107 | "metrics/$metricId/points", 108 | [ 109 | 'query' => [ 110 | 'per_page' => $num, 111 | 'current_page' => $page, 112 | ], 113 | ] 114 | ); 115 | } 116 | 117 | /** 118 | * Search if a defined number of Points exists. 119 | * 120 | * @param $metricId 121 | * @param string $search 122 | * @param string $by 123 | * @param int $limit 124 | * @param int $num 125 | * @param int $page 126 | * 127 | * @return mixed 128 | */ 129 | public function searchPoints($metricId, $search, $by, $limit = 1, $num = 1000, $page = 1) 130 | { 131 | $points = $this->indexPoints($metricId, $num, $page)['data']; 132 | 133 | $filtered = array_filter( 134 | $points, 135 | function ($point) use ($search, $by) { 136 | 137 | if (array_key_exists($by, $point)) { 138 | if (strpos($point[$by], $search) !== false) { 139 | return $point; 140 | } 141 | if ($point[$by] === $search) { 142 | return $point; 143 | } 144 | } 145 | 146 | return false; 147 | } 148 | ); 149 | 150 | if ($limit == 1) { 151 | return array_shift($filtered); 152 | } 153 | 154 | return array_slice($filtered, 0, $limit); 155 | } 156 | 157 | /** 158 | * Store a Point. 159 | * 160 | * @param int $metricId 161 | * @param array $point 162 | * 163 | * @return bool 164 | */ 165 | public function storePoint($metricId, array $point) 166 | { 167 | return $this->client->call('POST', "metrics/$metricId/points", ['json' => $point]); 168 | } 169 | 170 | /** 171 | * Delete a specific Point. 172 | * 173 | * @param int $metricId 174 | * @param int $PointId 175 | * 176 | * @return bool 177 | */ 178 | public function deletePoint($metricId, $PointId) 179 | { 180 | return $this->client->call('DELETE', "metrics/$metricId/points/$PointId"); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /src/Points/PointFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Points; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class PointFactory. 14 | * 15 | * The PointFactory generate a singleton for interact with Points in cachet 16 | */ 17 | class PointFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object PointActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new PointActions($client); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Subscribers/SubscriberActions.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Subscribers; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class SubscriberActions. 14 | * 15 | * The SubscriberActions contains all the action for Subscribers API available in CachetHQ 16 | */ 17 | class SubscriberActions 18 | { 19 | /** 20 | * Contains Cachet Client (using Guzzle). 21 | * 22 | * @var \GuzzleHttp\Client 23 | */ 24 | protected $client; 25 | 26 | /** 27 | * Contains cached Subscribers. 28 | * 29 | * @var null 30 | */ 31 | protected $cached = null; 32 | 33 | /** 34 | * Cache status. If is true use cache. 35 | * 36 | * @var bool 37 | */ 38 | private $cache = false; 39 | 40 | /** 41 | * SubscriberActions constructor. 42 | * 43 | * @param CachetClient $client 44 | */ 45 | public function __construct(CachetClient $client) 46 | { 47 | $this->client = $client; 48 | } 49 | 50 | /** 51 | * Set the cache to true or false. 52 | * 53 | * @param bool $status 54 | */ 55 | public function setCache($status) 56 | { 57 | $this->cache = $status; 58 | } 59 | 60 | /** 61 | * Cache Subscribers for performance improvement. 62 | * 63 | * @param int $num 64 | * @param int $page 65 | * 66 | * @return array|bool 67 | */ 68 | public function cacheSubscribers($num = 1000, $page = 1) 69 | { 70 | if ($this->cached != null) { 71 | return $this->cached; 72 | } 73 | 74 | $this->cached = $this->client->call( 75 | 'GET', 76 | 'subscribers', 77 | [ 78 | 'query' => [ 79 | 'per_page' => $num, 80 | 'current_page' => $page, 81 | ], 82 | ] 83 | ); 84 | 85 | return $this->cached; 86 | } 87 | 88 | /** 89 | * Get a defined number of Subscribers. 90 | * 91 | * @param int $num 92 | * @param int $page 93 | * 94 | * @return array|bool 95 | */ 96 | public function indexSubscribers($num = 1000, $page = 1) 97 | { 98 | if ($this->cache != false) { 99 | return $this->cacheSubscribers($num, $page); 100 | } 101 | 102 | return $this->client->call( 103 | 'GET', 104 | 'subscribers', 105 | [ 106 | 'query' => [ 107 | 'per_page' => $num, 108 | 'current_page' => $page, 109 | ], 110 | ] 111 | ); 112 | } 113 | 114 | /** 115 | * Search if a defined number of Subscribers exists. 116 | * 117 | * @param string $search 118 | * @param string $by 119 | * @param int $num 120 | * @param int $page 121 | * @param int $limit 122 | * 123 | * @return mixed 124 | */ 125 | public function searchSubscribers($search, $by, $limit = 1, $num = 1000, $page = 1) 126 | { 127 | $subscribers = $this->indexSubscribers($num, $page)['data']; 128 | 129 | $filtered = array_filter( 130 | $subscribers, 131 | function ($subscriber) use ($search, $by) { 132 | 133 | if (array_key_exists($by, $subscriber)) { 134 | if (strpos($subscriber[$by], $search) !== false) { 135 | return $subscriber; 136 | } 137 | if ($subscriber[$by] === $search) { 138 | return $subscriber; 139 | } 140 | } 141 | 142 | return false; 143 | } 144 | ); 145 | 146 | if ($limit == 1) { 147 | return array_shift($filtered); 148 | } 149 | 150 | return array_slice($filtered, 0, $limit); 151 | } 152 | 153 | /** 154 | * Store a Subscriber. 155 | * 156 | * @param array $subscriber 157 | * 158 | * @return bool 159 | */ 160 | public function storeSubscriber(array $subscriber) 161 | { 162 | return $this->client->call('POST', 'subscribers', ['json' => $subscriber]); 163 | } 164 | 165 | /** 166 | * Delete a specific Subscriber. 167 | * 168 | * @param int $id 169 | * 170 | * @return bool 171 | */ 172 | public function deleteSubscriber($id) 173 | { 174 | return $this->client->call('DELETE', "subscribers/$id"); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/Subscribers/SubscriberFactory.php: -------------------------------------------------------------------------------- 1 | 6 | */ 7 | 8 | namespace Damianopetrungaro\CachetSDK\Subscribers; 9 | 10 | use Damianopetrungaro\CachetSDK\CachetClient; 11 | 12 | /** 13 | * Class SubscriberFactory. 14 | * 15 | * The SubscriberFactory generate a singleton for interact with Subscribers in cachet 16 | */ 17 | class SubscriberFactory 18 | { 19 | /** 20 | * Build an object. 21 | * 22 | * @param $client 23 | * 24 | * @return object SubscriberActions 25 | */ 26 | public static function build(CachetClient $client) 27 | { 28 | return new SubscriberActions($client); 29 | } 30 | } 31 | --------------------------------------------------------------------------------