Commit | Line | Data |
---|---|---|
b4454468 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | namespace Civi\Pipe; | |
13 | ||
14 | /** | |
15 | * Collection of methods to expose to the pipe session. Any public method will be accessible. | |
16 | */ | |
17 | class PublicMethods { | |
18 | ||
f1ab7a2e TO |
19 | /** |
20 | * How should API errors be reported? | |
21 | * | |
22 | * @var string | |
23 | * - 'array': Traditional array format from civicrm_api(). Maximizes consistency of error data. | |
24 | * - 'exception': Converted to an exception. Somewhat lossy. Improves out-of-box DX on stricter JSON-RPC clients. | |
25 | */ | |
26 | protected $apiError = 'array'; | |
27 | ||
b4454468 TO |
28 | /** |
29 | * Send a request to APIv3. | |
30 | * | |
31 | * @param $session | |
32 | * @param array $request | |
33 | * Tuple: [$entity, $action, $params] | |
34 | * @return array|\Civi\Api4\Generic\Result|int | |
35 | */ | |
36 | public function api3($session, $request) { | |
37 | $request[2] = array_merge(['version' => 3, 'check_permissions' => TRUE], $request[2] ?? []); | |
f1ab7a2e TO |
38 | switch ($this->apiError) { |
39 | case 'array': | |
40 | return civicrm_api(...$request); | |
41 | ||
42 | case 'exception': | |
43 | return civicrm_api3(...$request); | |
44 | ||
45 | default: | |
46 | throw new \CRM_Core_Exception("Invalid API error-handling mode: $this->apiError"); | |
47 | } | |
b4454468 TO |
48 | } |
49 | ||
50 | /** | |
51 | * Send a request to APIv4. | |
52 | * | |
53 | * @param $session | |
54 | * @param array $request | |
55 | * Tuple: [$entity, $action, $params] | |
56 | * @return array|\Civi\Api4\Generic\Result|int | |
57 | */ | |
58 | public function api4($session, $request) { | |
59 | $request[2] = array_merge(['version' => 4, 'checkPermissions' => TRUE], $request[2] ?? []); | |
f1ab7a2e TO |
60 | switch ($this->apiError) { |
61 | case 'array': | |
62 | return civicrm_api(...$request); | |
63 | ||
64 | case 'exception': | |
65 | return civicrm_api4(...$request); | |
66 | ||
67 | default: | |
68 | throw new \CRM_Core_Exception("Invalid API error-handling mode: $this->apiError"); | |
69 | } | |
b4454468 TO |
70 | } |
71 | ||
72 | /** | |
73 | * Simple test; send/receive a fragment of data. | |
74 | * | |
75 | * @param $session | |
76 | * @param mixed $request | |
77 | * @return mixed | |
78 | */ | |
79 | public function echo($session, $request) { | |
80 | return $request; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Set active user. | |
85 | * | |
86 | * @param $session | |
87 | * @param array{contactId: int, userId: int, user: string} $request | |
88 | * @return array|\Civi\Api4\Generic\Result|int | |
89 | */ | |
90 | public function login($session, $request) { | |
91 | if (!function_exists('authx_login')) { | |
92 | throw new \CRM_Core_Exception("Cannot authenticate. Authx is not configured."); | |
93 | } | |
94 | $auth = authx_login($request, FALSE /* Pipe sessions do not need cookies or DB */); | |
95 | return \CRM_Utils_Array::subset($auth, ['contactId', 'userId']); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Set ephemeral session options. | |
100 | * | |
101 | * @param $session | |
102 | * @param array{maxLines: int, responsePrefix: int} $request | |
103 | * Any updates to perform. May be empty/omitted. | |
104 | * @return array{maxLines: int, responsePrefix: int} | |
105 | * List of updated options. | |
106 | * If the list of updates was empty, then return all options. | |
107 | */ | |
108 | public function options($session, $request) { | |
f1ab7a2e TO |
109 | $storageMap = [ |
110 | 'apiError' => $this, | |
b4454468 | 111 | 'maxLine' => $session, |
f1ab7a2e | 112 | 'responsePrefix' => $session, |
b4454468 TO |
113 | ]; |
114 | ||
f1ab7a2e TO |
115 | $get = function($storage, $name) { |
116 | if (method_exists($storage, 'get' . ucfirst($name))) { | |
117 | return $storage->{'get' . ucfirst($name)}(); | |
118 | } | |
119 | else { | |
120 | return $storage->{$name}; | |
121 | } | |
122 | }; | |
123 | ||
124 | $set = function($storage, $name, $value) use ($get) { | |
125 | if (method_exists($storage, 'set' . ucfirst($name))) { | |
126 | $storage->{'set' . ucfirst($name)}($value); | |
127 | } | |
128 | else { | |
129 | $storage->{$name} = $value; | |
130 | } | |
131 | return $get($storage, $name); | |
132 | }; | |
133 | ||
b4454468 TO |
134 | $result = []; |
135 | if (!empty($request)) { | |
f1ab7a2e TO |
136 | foreach ($request as $name => $value) { |
137 | if (isset($storageMap[$name])) { | |
138 | $result[$name] = $set($storageMap[$name], $name, $value); | |
b4454468 TO |
139 | } |
140 | } | |
141 | } | |
142 | else { | |
f1ab7a2e TO |
143 | foreach ($storageMap as $name => $storage) { |
144 | $result[$name] = $get($storage, $name); | |
b4454468 TO |
145 | } |
146 | } | |
147 | return $result; | |
148 | } | |
149 | ||
150 | } |