Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
10
11class Base
12{
13 const DIRECTORY_PAGE = '/rest/';
14
15 protected $query;
16 protected $page;
17 protected $directory;
18
19 private $token;
20
21 public function __construct()
22 {
23 $this->query = new Fields();
24
25 $this->initialize(new Integration\App\IntegrationB24());
26 }
27
28 public function setField($name, $value)
29 {
30 $this->query->set($name, $value);
31 return $this;
32 }
33 public function setFieldsValues($values)
34 {
35 if(is_array($values))
36 {
37 $this->query->setValues($values);
38 }
39 return $this;
40 }
41 public function getFieldsValues()
42 {
43 return $this->query->getValues();
44 }
45 public function setDirectory($directory)
46 {
47 $this->directory = $directory;
48 return $this;
49 }
50 public function setPageByType($type)
51 {
52 $registry = Registry::getRegistry();
53 $page = isset($registry[$type]) ? $registry[$type]:null;
54
55 if(is_null($page))
56 {
57 throw new ArgumentException("Unsupported cmd type: {$type}");
58 }
59
60 $this->setPage($page);
61 return $this;
62 }
63 public function setPage($page)
64 {
65 $this->page = $page;
66 return $this;
67 }
68 public function build()
69 {
70 $uri = new Uri($this->buildDirectoryPage());
71 return $uri
72 ->addParams($this->getFieldsValues())
73 ->getUri();
74 }
75 public function call()
76 {
77 $r = new Result();
78 try
79 {
80 $response = (new Integration\Rest\Client\TokenClient($this->token))
81 ->call(
82 $this->buildDirectoryPage(), $this->getFieldsValues());
83 }
84 catch (\Exception $exception)
85 {
86 return $r->addError(new Error("Error: ".$exception->getMessage()));
87 }
88
89 if (isset($response["error"]))
90 {
91 return $r->addError(new Error(
92 "Server Error: ".$response["error_description"]." (".$response["error"].")"
93 ));
94 }
95 else if (!isset($response["result"]))
96 {
97 return $r->addError(new Error("Wrong Server Response."));
98 }
99
100 return $r->setData(['DATA'=>$response]);
101 }
102 public function fill()
103 {
104 return $this;
105 }
106
107 protected function buildDirectoryPage()
108 {
109 return $this->directory.$this->page;
110 }
111
112 protected function initialize(Integration\App\Base $application)
113 {
114 $this->token = static::getAppToken($application->getCode());
115 }
116
117 private static function getAppToken($guid)
118 {
119 if (!is_string($guid) || empty($guid))
120 {
121 return null;
122 }
123
124 $token = Integration\Entity\B24integrationTokenTable::getList([
125 "select" => ["*"],
126 "filter" => ["=GUID" => $guid]
127 ])->fetchObject();
128
129 return $token ? $token : null;
130 }
131}
initialize(Integration\App\Base $application)
Definition base.php:112