vasadibt /
yii2-onesignal
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: Tamás |
||
| 5 | * Date: 2018. 11. 29. |
||
| 6 | * Time: 13:28 |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace vasadibt\onesignal\service; |
||
| 10 | |||
| 11 | use GuzzleHttp\Exception\GuzzleException; |
||
| 12 | use Psr\Http\Message\ResponseInterface; |
||
| 13 | use vasadibt\onesignal\OneSignal; |
||
| 14 | use yii\base\BaseObject; |
||
|
0 ignored issues
–
show
|
|||
| 15 | use yii\base\Exception; |
||
| 16 | use yii\base\InvalidConfigException; |
||
| 17 | use yii\helpers\Json; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class Request |
||
| 21 | * @package vasadibt\onesignal\service\base |
||
| 22 | * |
||
| 23 | * @property OneSignal $api |
||
| 24 | */ |
||
| 25 | class Request extends BaseObject |
||
| 26 | { |
||
| 27 | const GET = 'GET'; |
||
| 28 | const POST = 'POST'; |
||
| 29 | const PUT = 'PUT'; |
||
| 30 | const DELETE = 'DELETE'; |
||
| 31 | |||
| 32 | const APPS = 'apps'; |
||
| 33 | const PLAYERS = 'players'; |
||
| 34 | const NOTIFICATIONS = 'notifications'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var OneSignal |
||
| 38 | */ |
||
| 39 | public $api; |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $basicAuthKey; |
||
| 44 | /** |
||
| 45 | * @var string api endpoint name |
||
| 46 | */ |
||
| 47 | public $methodName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | * @throws InvalidConfigException |
||
| 52 | */ |
||
| 53 | public function init() |
||
| 54 | { |
||
| 55 | parent::init(); |
||
| 56 | |||
| 57 | if (empty($this->api)) { |
||
| 58 | throw new InvalidConfigException("The 'api' option is required."); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (empty($this->basicAuthKey)) { |
||
| 62 | throw new InvalidConfigException("The 'basicAuthKey' option is required."); |
||
| 63 | } |
||
| 64 | if (empty($this->methodName)) { |
||
| 65 | throw new InvalidConfigException("The 'methodName' option is required."); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Make a request to the OneSignal API |
||
| 71 | * @param string $method |
||
| 72 | * @param string $uri |
||
| 73 | * @param array $data |
||
| 74 | * @return array |
||
| 75 | * @throws Exception |
||
| 76 | */ |
||
| 77 | public function request($method, $uri = null, $data = []) |
||
| 78 | { |
||
| 79 | $uri = rtrim($this->api->apiUrl . '/' . $this->methodName . '/' . $uri, '/'); |
||
| 80 | $this->api->log('Create api request: ' . $uri, __METHOD__); |
||
| 81 | |||
| 82 | $this->api->log('Request data: ' . PHP_EOL . Json::encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), __METHOD__); |
||
| 83 | |||
| 84 | if (!$this->api->enabled) { |
||
| 85 | $this->api->log('Api is disabled', __METHOD__); |
||
| 86 | return []; |
||
| 87 | } |
||
| 88 | |||
| 89 | $queryKey = in_array($method, [static::GET, static::DELETE]) ? 'query' : 'json'; |
||
| 90 | $options = [ |
||
| 91 | 'headers' => ['Authorization' => 'Basic ' . $this->basicAuthKey], |
||
| 92 | $queryKey => array_merge(['app_id' => $this->api->appId], $data), |
||
| 93 | ]; |
||
| 94 | |||
| 95 | // make signature |
||
| 96 | try { |
||
| 97 | $response = $this->api->client->request($method, $uri, $options); |
||
| 98 | } catch (GuzzleException $e) { |
||
| 99 | $this->api->log('Api exception: ' . $e->getMessage(), __METHOD__); |
||
| 100 | if (method_exists($e, 'getResponse')) { |
||
| 101 | $this->api->log($this->responseFormat($e->getResponse()), __METHOD__); |
||
| 102 | } |
||
| 103 | throw new Exception($e->getMessage(), $e->getCode(), $e); |
||
| 104 | } |
||
| 105 | |||
| 106 | try { |
||
| 107 | $jsonData = Json::decode($response->getBody()); |
||
| 108 | } catch (\Throwable $e) { |
||
| 109 | throw new Exception($e->getMessage(), $e->getCode(), $e); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->api->log('Success request', __METHOD__); |
||
| 113 | $this->api->log(Json::encode($jsonData, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), __METHOD__); |
||
| 114 | |||
| 115 | return $jsonData; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string|ResponseInterface $response |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function responseFormat($response) |
||
| 123 | { |
||
| 124 | if ($response instanceof ResponseInterface) { |
||
| 125 | $body = $response->getBody(); |
||
| 126 | $response = static::isJson($body) |
||
| 127 | ? Json::encode(Json::decode($body), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) |
||
| 128 | : $body; |
||
| 129 | } |
||
| 130 | return $response; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * GET |
||
| 135 | * @param $uri |
||
| 136 | * @param array $data |
||
| 137 | * @return array |
||
| 138 | * @throws Exception |
||
| 139 | */ |
||
| 140 | public function get($uri = null, $data = []) |
||
| 141 | { |
||
| 142 | return $this->request(static::GET, $uri, $data); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * POST |
||
| 147 | * @param $uri |
||
| 148 | * @param array $data |
||
| 149 | * @return array |
||
| 150 | * @throws Exception |
||
| 151 | */ |
||
| 152 | public function post($uri = null, $data = []) |
||
| 153 | { |
||
| 154 | return $this->request(static::POST, $uri, $data); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * PUT |
||
| 159 | * @param $uri |
||
| 160 | * @param array $data |
||
| 161 | * @return array |
||
| 162 | * @throws Exception |
||
| 163 | */ |
||
| 164 | public function put($uri = null, $data = []) |
||
| 165 | { |
||
| 166 | return $this->request(static::PUT, $uri, $data); |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * DELETE |
||
| 172 | * @param $uri |
||
| 173 | * @param array $data |
||
| 174 | * @return array |
||
| 175 | * @throws Exception |
||
| 176 | */ |
||
| 177 | public function delete($uri = null, $data = []) |
||
| 178 | { |
||
| 179 | return $this->request(static::DELETE, $uri, $data); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $string |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public static function isJson($string) |
||
| 187 | { |
||
| 188 | json_decode($string); |
||
| 189 | return (json_last_error() == JSON_ERROR_NONE); |
||
| 190 | } |
||
| 191 | } |
||
| 192 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths