Civi::pipe - Add some initial utilities
[civicrm-core.git] / Civi / Pipe / PublicMethods.php
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
19 /**
20 * Send a request to APIv3.
21 *
22 * @param $session
23 * @param array $request
24 * Tuple: [$entity, $action, $params]
25 * @return array|\Civi\Api4\Generic\Result|int
26 */
27 public function api3($session, $request) {
28 $request[2] = array_merge(['version' => 3, 'check_permissions' => TRUE], $request[2] ?? []);
29 return civicrm_api(...$request);
30 }
31
32 /**
33 * Send a request to APIv4.
34 *
35 * @param $session
36 * @param array $request
37 * Tuple: [$entity, $action, $params]
38 * @return array|\Civi\Api4\Generic\Result|int
39 */
40 public function api4($session, $request) {
41 $request[2] = array_merge(['version' => 4, 'checkPermissions' => TRUE], $request[2] ?? []);
42 return civicrm_api(...$request);
43 }
44
45 /**
46 * Simple test; send/receive a fragment of data.
47 *
48 * @param $session
49 * @param mixed $request
50 * @return mixed
51 */
52 public function echo($session, $request) {
53 return $request;
54 }
55
56 /**
57 * Set active user.
58 *
59 * @param $session
60 * @param array{contactId: int, userId: int, user: string} $request
61 * @return array|\Civi\Api4\Generic\Result|int
62 */
63 public function login($session, $request) {
64 if (!function_exists('authx_login')) {
65 throw new \CRM_Core_Exception("Cannot authenticate. Authx is not configured.");
66 }
67 $auth = authx_login($request, FALSE /* Pipe sessions do not need cookies or DB */);
68 return \CRM_Utils_Array::subset($auth, ['contactId', 'userId']);
69 }
70
71 /**
72 * Set ephemeral session options.
73 *
74 * @param $session
75 * @param array{maxLines: int, responsePrefix: int} $request
76 * Any updates to perform. May be empty/omitted.
77 * @return array{maxLines: int, responsePrefix: int}
78 * List of updated options.
79 * If the list of updates was empty, then return all options.
80 */
81 public function options($session, $request) {
82 $map = [
83 'responsePrefix' => $session,
84 'maxLine' => $session,
85 ];
86
87 $result = [];
88 if (!empty($request)) {
89 foreach ($request as $option => $value) {
90 if (isset($map[$option])) {
91 $storage = $map[$option];
92 $storage->{'set' . ucfirst($option)}($value);
93 $result[$option] = $storage->{'get' . ucfirst($option)}();
94 }
95 }
96 }
97 else {
98 foreach ($map as $option => $storage) {
99 $result[$option] = $storage->{'get' . ucfirst($option)}();
100 }
101 }
102 return $result;
103 }
104
105 }