Contracts
5 years ago
Enum
5 years ago
Events
5 years ago
Helpers
5 years ago
Repositories
5 years ago
TrackingData
5 years ago
AccessToken.php
5 years ago
AdminActionHandler.php
5 years ago
AdminSettings.php
5 years ago
TrackClient.php
5 years ago
TrackJob.php
5 years ago
TrackJobScheduler.php
5 years ago
TrackRegisterer.php
5 years ago
TrackingServiceProvider.php
5 years ago
UsageTrackingOnBoarding.php
5 years ago
AccessToken.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Tracking; |
| 4 | |
| 5 | use Give\Tracking\Enum\EventType; |
| 6 | use Give\Tracking\Repositories\TelemetryAccessDetails; |
| 7 | use Give\Tracking\TrackingData\WebsiteInfoData; |
| 8 | |
| 9 | /** |
| 10 | * Class AccessTokenGenerator |
| 11 | * @package Give\Tracking |
| 12 | * |
| 13 | * @since 2.10.0 |
| 14 | */ |
| 15 | class AccessToken { |
| 16 | /** |
| 17 | * @var TrackClient |
| 18 | */ |
| 19 | private $trackClient; |
| 20 | |
| 21 | /** |
| 22 | * @var TelemetryAccessDetails |
| 23 | */ |
| 24 | private $telemetryAccessDetails; |
| 25 | |
| 26 | /** |
| 27 | * AccessToken constructor. |
| 28 | * |
| 29 | * @param TrackClient $trackClient |
| 30 | * @param TelemetryAccessDetails $telemetryAccessDetails |
| 31 | */ |
| 32 | public function __construct( TrackClient $trackClient, TelemetryAccessDetails $telemetryAccessDetails ) { |
| 33 | $this->trackClient = $trackClient; |
| 34 | $this->telemetryAccessDetails = $telemetryAccessDetails; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Store access token |
| 39 | * |
| 40 | * @since 2.10.0 |
| 41 | */ |
| 42 | public function store() { |
| 43 | /* @var WebsiteInfoData $dataClass */ |
| 44 | $dataClass = give( WebsiteInfoData::class ); |
| 45 | |
| 46 | $response = $this->trackClient->post( new EventType( EventType::CREATE_TOKEN ), $dataClass, [ 'blocking' => true ] ); |
| 47 | if ( is_wp_error( $response ) ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | $response = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 52 | if ( empty( $response['success'] ) ) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | $token = $response['data']['access_token']; |
| 57 | $this->telemetryAccessDetails->saveAccessTokenOptionValue( $token ); |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 |