|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Integrations\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Log; |
|
7
|
|
|
use App\Models\User; |
|
8
|
|
|
|
|
9
|
|
|
use Integrations\Models\Token; |
|
10
|
|
|
use Support\Components\Coders\Parser\ParseClass; |
|
11
|
|
|
use Muleta\Utils\Debugger\ErrorHelper; |
|
12
|
|
|
use Integrations\Models\Integration as IntegrationModel; |
|
13
|
|
|
use ReflectionGenerator; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use Muleta\Utils\Extratores\ClasserExtractor; |
|
16
|
|
|
use Muleta\Contracts\Output\OutputableTrait; |
|
17
|
|
|
|
|
18
|
|
|
class Connector |
|
19
|
|
|
{ |
|
20
|
|
|
use OutputableTrait; |
|
21
|
|
|
|
|
22
|
|
|
protected $_connection = null; |
|
23
|
|
|
|
|
24
|
|
|
protected $_token = null; |
|
25
|
|
|
|
|
26
|
|
|
private $error = null; |
|
27
|
|
|
|
|
28
|
|
|
private $errorCode = null; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($token = false) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->_token = $token; |
|
33
|
|
|
$this->_connection = $this->getConnection($this->_token); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Recupera connecção com a integração |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getPrimary() |
|
40
|
|
|
{ |
|
41
|
|
|
return static::$ID; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Recupera connecção com a integração |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getConnection($token = false) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setError($errorMessage, $code = 0) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->error = $errorMessage; |
|
55
|
|
|
$this->errorCode = $code; |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
var_dump($errorMessage); |
|
|
|
|
|
|
59
|
|
|
throw new \Exception($errorMessage); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getError() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->error; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getErrorCode() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->errorCode; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Recupera dados em cima de um get de uma api |
|
74
|
|
|
*/ |
|
75
|
|
|
public function get($path) |
|
76
|
|
|
{ |
|
77
|
|
|
// @todo Fazer resultar um object em cia de um path |
|
78
|
|
|
$url = $this->url.$path; |
|
|
|
|
|
|
79
|
|
|
$result = []; |
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Recupera dados em cima de um get de uma api |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function getCodeForPrimaryKey() |
|
87
|
|
|
{ |
|
88
|
|
|
$integration = IntegrationModel::createIfNotExistAndReturn( |
|
89
|
|
|
[ |
|
90
|
|
|
'id' => static::$ID, |
|
91
|
|
|
'name' => ClasserExtractor::getClassName(static::class), |
|
92
|
|
|
'code' => static::class, |
|
93
|
|
|
] |
|
94
|
|
|
); |
|
95
|
|
|
return $integration->id; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function registerAll() |
|
99
|
|
|
{ |
|
100
|
|
|
$realPath = __DIR__.'/'; |
|
101
|
|
|
|
|
102
|
|
|
collect(scandir($realPath)) |
|
103
|
|
|
->each( |
|
104
|
|
|
function ($item) use ($realPath) { |
|
105
|
|
|
if (in_array($item, ['.', '..'])) { return; |
|
106
|
|
|
} |
|
107
|
|
|
if (is_dir($realPath . $item)) { |
|
108
|
|
|
$modelName = __NAMESPACE__.'\\'.$item.'\\'.$item; |
|
109
|
|
|
|
|
110
|
|
|
IntegrationModel::createIfNotExistAndReturn( |
|
111
|
|
|
[ |
|
112
|
|
|
'id' => call_user_func(array($modelName, 'getPrimary')), |
|
113
|
|
|
'name' => ClasserExtractor::getClassName($modelName), |
|
114
|
|
|
'code' => $modelName, |
|
115
|
|
|
] |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (is_file($realPath . $item) && $item!=='Connector.php') { |
|
120
|
|
|
Log::channel('sitec-integrations')->warning( |
|
121
|
|
|
ErrorHelper::tratarMensagem( |
|
122
|
|
|
'Não deveria ter arquivo nessa pasta: '.$realPath . $item |
|
123
|
|
|
) |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
try { |
|
127
|
|
|
throw new Exception('Não deveria ter arquivo nessa pasta: '.$realPath . $item); |
|
128
|
|
|
} catch(Exception $e) { |
|
129
|
|
|
dd('Connector', $e->getTrace(), $e->getMessage(), 'Deu Ruim Integrations'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|