PluginProbe
seQura / 3.0.2
seQura v3.0.2
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Controllers / Rest / class-log-rest-controller.php

class-log-rest-controller.php in seQura 3.0.2, at src/Controllers/Rest/class-log-rest-controller.php

143 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST Log Controller
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Controllers/Rest
7 */
8
9 namespace SeQura\WC\Controllers\Rest;
10
11 use SeQura\WC\Services\Interface_Logger_Service;
12 use WP_Error;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 /**
17 * REST Log Controller
18 */
19 class Log_REST_Controller extends REST_Controller {
20
21 const PARAM_IS_ENABLED = 'isEnabled';
22 const PARAM_LOG_LEVEL = 'level';
23
24 /**
25 * Constructor.
26 *
27 * @param string $rest_namespace The namespace.
28 * @param Interface_Logger_Service $logger The logger service.
29 */
30 public function __construct( $rest_namespace, Interface_Logger_Service $logger ) {
31 parent::__construct( $logger );
32 $this->namespace = $rest_namespace;
33 $this->rest_base = '/log';
34 }
35
36 /**
37 * Register the API endpoints.
38 */
39 public function register_routes(): void {
40 $this->logger->log_info( 'Hook executed', __FUNCTION__, __CLASS__ );
41 $store_id_args = array( self::PARAM_STORE_ID => $this->get_arg_string() );
42 $conf_args = array_merge(
43 $store_id_args,
44 array(
45 self::PARAM_IS_ENABLED => $this->get_arg_bool(),
46 self::PARAM_LOG_LEVEL => $this->get_arg_int(),
47 )
48 );
49 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
50
51 $this->register_get( "{$store_id}", 'get_logs', $store_id_args );
52 $this->register_delete( "{$store_id}", 'delete_logs', $store_id_args );
53
54 $this->register_get( "settings/{$store_id}", 'get_configuration', $store_id_args );
55 $this->register_post( "settings/{$store_id}", 'save_configuration', $conf_args );
56 }
57
58 /**
59 * GET logs.
60 *
61 * @param WP_REST_Request $request The request.
62 *
63 * @return WP_REST_Response|WP_Error
64 */
65 public function get_logs( WP_REST_Request $request ) {
66 $response = null;
67 try {
68 $response = $this->logger->get_content(
69 $request->get_param( self::PARAM_STORE_ID )
70 );
71 } catch ( \Throwable $e ) {
72 $response = new WP_Error( 'error', $e->getMessage() );
73 }
74 return rest_ensure_response( $response );
75 }
76
77 /**
78 * DELETE logs.
79 *
80 * @param WP_REST_Request $request The request.
81 *
82 * @return WP_REST_Response|WP_Error
83 */
84 public function delete_logs( WP_REST_Request $request ) {
85 $response = null;
86 try {
87 $this->logger->clear(
88 $request->get_param( self::PARAM_STORE_ID )
89 );
90 $response = array();
91 } catch ( \Throwable $e ) {
92 $response = new WP_Error( 'error', $e->getMessage() );
93 }
94 return rest_ensure_response( $response );
95 }
96
97 /**
98 * GET logs configuration.
99 *
100 * @return WP_REST_Response|WP_Error
101 */
102 public function get_configuration() {
103 $response = null;
104 try {
105 $response = $this->get_config_response();
106 } catch ( \Throwable $e ) {
107 $response = new WP_Error( 'error', $e->getMessage() );
108 }
109 return rest_ensure_response( $response );
110 }
111
112 /**
113 * POST logs configuration.
114 *
115 * @param WP_REST_Request $request The request.
116 *
117 * @return WP_REST_Response|WP_Error
118 */
119 public function save_configuration( WP_REST_Request $request ) {
120 $response = null;
121 try {
122 $this->logger->enable( $request->get_param( self::PARAM_IS_ENABLED ) );
123 $this->logger->set_min_log_level( $request->get_param( self::PARAM_LOG_LEVEL ) );
124 $response = $this->get_config_response();
125 } catch ( \Throwable $e ) {
126 $response = new WP_Error( 'error', $e->getMessage() );
127 }
128 return rest_ensure_response( $response );
129 }
130
131 /**
132 * Make the logger configuration response.
133 *
134 * @return array<string, mixed>
135 */
136 private function get_config_response(): array {
137 return array(
138 self::PARAM_IS_ENABLED => $this->logger->is_enabled(),
139 self::PARAM_LOG_LEVEL => $this->logger->get_min_log_level(),
140 );
141 }
142 }
143