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