| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace Give\Tracking; |
| 4 | 3 | |
| 5 | 4 | use Give\Tracking\Contracts\TrackData; |
| 6 | 5 | use Give\Tracking\Enum\EventType; |
| @@ -11,77 +10,73 @@ | ||
| 11 | 10 | * Class TrackClient |
| 12 | 11 | * |
| 13 | 12 | * This class has responsibility to send tracking information |
| 14 | 13 | * |
| 14 | + * @since 2.10.0 | |
| 15 | 15 | * @package Give\Tracking |
| 16 | 16 | * |
| 17 | - * @since 2.10.0 | |
| 18 | 17 | */ |
| 19 | -class TrackClient | |
| 20 | -{ | |
| 21 | - const SERVER_URL = 'https://telemetry.givewp.com/api/v1/track-plugin-usage'; | |
| 18 | +class TrackClient { | |
| 19 | + const SERVER_URL = 'https://telemetry.givewp.com/api/v1/track-plugin-usage'; | |
| 22 | 20 | |
| 23 | - /** | |
| 24 | - * @var TelemetryAccessDetails | |
| 25 | - */ | |
| 26 | - private $telemetryAccessDetails; | |
| 21 | + /** | |
| 22 | + * @var TelemetryAccessDetails | |
| 23 | + */ | |
| 24 | + private $telemetryAccessDetails; | |
| 27 | 25 | |
| 28 | - /** | |
| 29 | - * TrackClient constructor. | |
| 30 | - * | |
| 31 | - * @param TelemetryAccessDetails $telemetryAccessDetails | |
| 32 | - */ | |
| 33 | - public function __construct(TelemetryAccessDetails $telemetryAccessDetails) | |
| 34 | - { | |
| 35 | - $this->telemetryAccessDetails = $telemetryAccessDetails; | |
| 36 | - } | |
| 26 | + /** | |
| 27 | + * TrackClient constructor. | |
| 28 | + * | |
| 29 | + * @param TelemetryAccessDetails $telemetryAccessDetails | |
| 30 | + */ | |
| 31 | + public function __construct( TelemetryAccessDetails $telemetryAccessDetails ) { | |
| 32 | + $this->telemetryAccessDetails = $telemetryAccessDetails; | |
| 33 | + } | |
| 37 | 34 | |
| 38 | - /** | |
| 39 | - * Send a track event. | |
| 40 | - * | |
| 41 | - * @since 2.10.0 | |
| 42 | - * | |
| 43 | - * @param EventType $eventType | |
| 44 | - * @param TrackData $trackData | |
| 45 | - * @param array $requestArgs | |
| 46 | - * | |
| 47 | - * @return array|WP_Error | |
| 48 | - */ | |
| 49 | - public function post(EventType $eventType, TrackData $trackData, $requestArgs = []) | |
| 50 | - { | |
| 51 | - $id = $eventType->getValue(); | |
| 52 | - $data = $trackData->get(); | |
| 35 | + /** | |
| 36 | + * Send a track event. | |
| 37 | + * | |
| 38 | + * @since 2.10.0 | |
| 39 | + * | |
| 40 | + * @param EventType $eventType | |
| 41 | + * @param TrackData $trackData | |
| 42 | + * @param array $requestArgs | |
| 43 | + * | |
| 44 | + * @return array|WP_Error | |
| 45 | + */ | |
| 46 | + public function post( EventType $eventType, TrackData $trackData, $requestArgs = [] ) { | |
| 47 | + $id = $eventType->getValue(); | |
| 48 | + $data = $trackData->get(); | |
| 53 | 49 | |
| 54 | - if ( ! $id || ! $data) { | |
| 55 | - return new WP_Error('invalid-telemetry-request', 'Pass valid track id and tracked data to TrackClient'); | |
| 56 | - } | |
| 50 | + if ( ! $id || ! $data ) { | |
| 51 | + return new WP_Error( 'invalid-telemetry-request', 'Pass valid track id and tracked data to TrackClient' ); | |
| 52 | + } | |
| 57 | 53 | |
| 58 | - $default_request_args = [ | |
| 59 | - 'headers' => [ | |
| 60 | - 'content-type:' => 'application/json', | |
| 61 | - 'Authorization' => 'Bearer ' . $this->telemetryAccessDetails->getAccessTokenOptionValue(), | |
| 62 | - ], | |
| 63 | - 'timeout' => 8, | |
| 64 | - 'httpversion' => '1.1', | |
| 65 | - 'blocking' => false, | |
| 66 | - 'user-agent' => 'GIVE/' . GIVE_VERSION . ' ' . get_bloginfo('url'), | |
| 67 | - 'body' => wp_json_encode($data), | |
| 68 | - 'data_format' => 'body', | |
| 69 | - ]; | |
| 54 | + $default_request_args = [ | |
| 55 | + 'headers' => [ | |
| 56 | + 'content-type:' => 'application/json', | |
| 57 | + 'Authorization' => 'Bearer ' . $this->telemetryAccessDetails->getAccessTokenOptionValue(), | |
| 58 | + ], | |
| 59 | + 'timeout' => 8, | |
| 60 | + 'httpversion' => '1.1', | |
| 61 | + 'blocking' => false, | |
| 62 | + 'user-agent' => 'GIVE/' . GIVE_VERSION . ' ' . get_bloginfo( 'url' ), | |
| 63 | + 'body' => wp_json_encode( $data ), | |
| 64 | + 'data_format' => 'body', | |
| 65 | + ]; | |
| 70 | 66 | |
| 71 | - return wp_remote_post($this->getApiUrl($id), wp_parse_args($requestArgs, $default_request_args)); | |
| 72 | - } | |
| 67 | + return wp_remote_post( $this->getApiUrl( $id ), wp_parse_args( $requestArgs, $default_request_args ) ); | |
| 68 | + } | |
| 73 | 69 | |
| 74 | - /** | |
| 75 | - * Get api url. | |
| 76 | - * | |
| 77 | - * @since 2.10.0 | |
| 78 | - * | |
| 79 | - * @param string $trackId | |
| 80 | - * | |
| 81 | - * @return string | |
| 82 | - */ | |
| 83 | - public function getApiUrl($trackId) | |
| 84 | - { | |
| 85 | - return self::SERVER_URL . '/' . $trackId; | |
| 86 | - } | |
| 70 | + /** | |
| 71 | + * Get api url. | |
| 72 | + * | |
| 73 | + * @since 2.10.0 | |
| 74 | + * | |
| 75 | + * @param string $trackId | |
| 76 | + * | |
| 77 | + * @return string | |
| 78 | + */ | |
| 79 | + public function getApiUrl( $trackId ) { | |
| 80 | + return self::SERVER_URL . '/' . $trackId; | |
| 81 | + } | |
| 87 | 82 | } |