dynamikaweb /
yii2-1doc-api
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * |
||||
| 4 | * @copyright Copyright (c) 2019 Dynamika Web |
||||
| 5 | * @link https://github.com/dynamikaweb/yii2-1doc-api |
||||
| 6 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
||||
| 7 | * |
||||
| 8 | */ |
||||
| 9 | namespace dynamikaweb\api; |
||||
| 10 | |||||
| 11 | use Yii; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * |
||||
| 15 | * @api 1Doc |
||||
| 16 | * @author Rodrigo Dornelles <[email protected]> <[email protected]> |
||||
| 17 | * @version 0.1a (18/09/2019) |
||||
| 18 | * |
||||
| 19 | * * |
||||
| 20 | * |
||||
| 21 | * @see todos os comentarios estão em português por que o uso desta @api está focado no mercado nacional. |
||||
| 22 | * |
||||
| 23 | * * |
||||
| 24 | * |
||||
| 25 | */ |
||||
| 26 | class OneDocApi extends \yii\base\Model |
||||
| 27 | { |
||||
| 28 | /** |
||||
| 29 | * @property XXX |
||||
| 30 | * @return string |
||||
| 31 | * |
||||
| 32 | * destinado a ocultação de informações sensiveis |
||||
| 33 | * |
||||
| 34 | */ |
||||
| 35 | const XXX = 'XXXXXXXXXX'; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @property URLS |
||||
| 39 | * @return array |
||||
| 40 | * |
||||
| 41 | * urls padrões pré estabelecidas |
||||
| 42 | * |
||||
| 43 | */ |
||||
| 44 | const URLS = [ |
||||
| 45 | 'prod' => 'https://api.1doc.com.br', |
||||
| 46 | 'testes' => 'https://app7.1doc.com.br/api/v2/' |
||||
| 47 | ]; |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @property instructs |
||||
| 51 | * @return array |
||||
| 52 | * |
||||
| 53 | * requisição que sera feita pela @api |
||||
| 54 | * |
||||
| 55 | */ |
||||
| 56 | public $instructs = [ |
||||
| 57 | 'method' => '', |
||||
| 58 | 'client_auth' => self::XXX, |
||||
| 59 | 'token' => self::XXX, |
||||
| 60 | 'secret' => self::XXX, |
||||
| 61 | ]; |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @property rules |
||||
| 65 | * @return array-array |
||||
| 66 | * |
||||
| 67 | * regras de monstagem da @property instruct |
||||
| 68 | * |
||||
| 69 | */ |
||||
| 70 | public $rules = [ |
||||
| 71 | 'emissao' => [ |
||||
| 72 | 'hash' => 'hash', |
||||
| 73 | 'grupo' => 'grupo', |
||||
| 74 | 'documento' => 'id_documento', |
||||
| 75 | 'disponivel' => 'disponivel_workplace', |
||||
| 76 | 'origem' => 'origem_id_pessoa', |
||||
| 77 | 'destino' => 'destino_id_pessoa', |
||||
| 78 | 'cidade' => 'cidade' |
||||
| 79 | ], |
||||
| 80 | 'pagina' => 'num_pagina', |
||||
| 81 | 'limit' => 'limit' |
||||
| 82 | ]; |
||||
| 83 | |||||
| 84 | /** |
||||
| 85 | * @property url_request |
||||
| 86 | * @return string |
||||
| 87 | * |
||||
| 88 | * url para requisição restfull @api |
||||
| 89 | * |
||||
| 90 | */ |
||||
| 91 | private $url_request; |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * @property data |
||||
| 95 | * @return array |
||||
| 96 | * |
||||
| 97 | * dados retornados pela @api da 1doc |
||||
| 98 | * |
||||
| 99 | */ |
||||
| 100 | public $data = []; |
||||
| 101 | |||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * __construct |
||||
| 105 | * |
||||
| 106 | * quando component for montado, já deve ser criado component auxiliar para autenticação segura |
||||
| 107 | * |
||||
| 108 | * @return void |
||||
| 109 | */ |
||||
| 110 | public function __construct( $config = [] ) |
||||
| 111 | { |
||||
| 112 | /** |
||||
| 113 | * @see componet dynamikaweb\OneDocAuth |
||||
| 114 | */ |
||||
| 115 | Yii::$app->set( 'OneDocAuth', new OneDocAuth ); |
||||
| 116 | parent::__construct( $config ); |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * init |
||||
| 121 | * |
||||
| 122 | * quando componet for chamado pela primeira vez |
||||
| 123 | * |
||||
| 124 | * @return void |
||||
| 125 | */ |
||||
| 126 | public function init ( ) |
||||
| 127 | { |
||||
| 128 | // Define uma URL padrão para quando não foi escolhida |
||||
| 129 | if ( !$this->url_request ) { |
||||
| 130 | $url = self::URLS; |
||||
| 131 | $this->url_request = current($url); |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * find |
||||
| 138 | * |
||||
| 139 | * montagem da estrutura de request de acordo com @property rules |
||||
| 140 | * |
||||
| 141 | * @param array $options |
||||
| 142 | * |
||||
| 143 | * @return array $this->instructs |
||||
| 144 | */ |
||||
| 145 | public function find( $options ) |
||||
| 146 | { |
||||
| 147 | foreach ($options as $option => $value){ |
||||
| 148 | // se o parametro for o metodo, colocar prefixo emission |
||||
| 149 | if($option == 'method'){ |
||||
| 150 | $this->instructs['method'] = 'emission'.ucfirst($value); |
||||
| 151 | continue; |
||||
| 152 | } |
||||
| 153 | // parametros que estão em emissão |
||||
| 154 | if( isset($this->rules['emissao'][$option]) ){ |
||||
| 155 | $this->instructs['emissao'][$this->rules['emissao'][$option]] = $value; |
||||
| 156 | continue; |
||||
| 157 | } |
||||
| 158 | // parametros aceitos fora de emissão |
||||
| 159 | if( isset($this->rules[$option]) ){ |
||||
| 160 | $this->instructs[$this->rules[$option]] = $value; |
||||
| 161 | continue; |
||||
| 162 | } |
||||
| 163 | } |
||||
| 164 | return $this->instructs; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * getRun |
||||
| 170 | * |
||||
| 171 | * executa a requisição para @api 1DOC |
||||
| 172 | * |
||||
| 173 | * @return array-object |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 174 | */ |
||||
| 175 | public function getRun() |
||||
| 176 | { |
||||
| 177 | $ch= curl_init(); |
||||
| 178 | curl_setopt($ch, CURLOPT_URL, $this->url_request); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 179 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
| 180 | curl_setopt($ch, CURLOPT_POST, 1); |
||||
| 181 | curl_setopt($ch, CURLOPT_POSTFIELDS, "data=". base64_encode($this->getRequest(true))); |
||||
| 182 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); |
||||
| 183 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
| 184 | $this->data = json_decode(curl_exec ($ch)); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 185 | curl_close($ch); |
||||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 186 | return $this; |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | /** |
||||
| 190 | * getRequest |
||||
| 191 | * |
||||
| 192 | * @param boolean $real Paremtros de autenticação são reais ( por padrão falso para esconder a informação ) |
||||
| 193 | * |
||||
| 194 | * @return string-json a requisão que sera feita para api 1doc |
||||
|
0 ignored issues
–
show
|
|||||
| 195 | */ |
||||
| 196 | public function getRequest( $real = false ) |
||||
| 197 | { |
||||
| 198 | $json = $this->instructs; |
||||
| 199 | |||||
| 200 | // se for uma requisição real, substiuir autenticação ficticia pela verdadeira |
||||
| 201 | if( $real ){ |
||||
| 202 | $auth = Yii::$app->OneDocAuth; // objeto de autenticação |
||||
| 203 | $json = \yii\helpers\ArrayHelper::merge( $json, $auth() ); |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | return \yii\helpers\Json::encode( $json, false ); |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | /** |
||||
| 210 | * __set |
||||
| 211 | * |
||||
| 212 | * @param string $name |
||||
| 213 | * @param mixed $value |
||||
| 214 | * |
||||
| 215 | */ |
||||
| 216 | public function __set( $name, $value ) |
||||
| 217 | { |
||||
| 218 | switch ($name) |
||||
| 219 | { |
||||
| 220 | case 'client_auth': case 'token': case 'secret': |
||||
| 221 | Yii::$app->OneDocAuth->$name = $value; |
||||
| 222 | return ; |
||||
| 223 | case 'url': |
||||
| 224 | $urls = self::URLS; |
||||
| 225 | $this->url_request = !isset($urls[$value]) ? $value:$urls[$value]; |
||||
| 226 | return ; |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | parent::__set( $name, $value ); |
||||
| 230 | } |
||||
| 231 | } |
||||
| 232 |