Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TrackingExtensionDeployEventListener | |
0.00% |
0 / 72 |
|
0.00% |
0 / 4 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 49 |
|
0.00% |
0 / 1 |
182 | |||
| getSiteNameFromUrl | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| updateSync | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Listeners; |
| 4 | |
| 5 | use Exception; |
| 6 | use Carbon\Carbon; |
| 7 | use App\Http\Models\Auth\User; |
| 8 | use App\Http\Models\FlyCutUsage; |
| 9 | use App\Http\Models\HubspotProperties; |
| 10 | use App\Http\Models\FlymsgDeploymentRecord; |
| 11 | use App\Events\TrackingExtensionDeployEvent; |
| 12 | use Symfony\Component\HttpFoundation\Response; |
| 13 | use App\Http\Models\Shortcut; |
| 14 | use App\Http\Models\UserInfo; |
| 15 | use App\Http\Services\StatisticsService; |
| 16 | use App\Services\UserInfo\UserInfoService; |
| 17 | use Illuminate\Support\Facades\Log; |
| 18 | |
| 19 | class TrackingExtensionDeployEventListener |
| 20 | { |
| 21 | public function __construct( |
| 22 | private StatisticsService $statisticsService |
| 23 | ) {} |
| 24 | |
| 25 | public function handle(TrackingExtensionDeployEvent $event) |
| 26 | { |
| 27 | // Access the parameters within the job's handle method |
| 28 | $user = $event->user; |
| 29 | $data = $event->data; |
| 30 | |
| 31 | $shortcut = $event->shortcut; |
| 32 | $browser_type = $event->browserType; |
| 33 | |
| 34 | if ($shortcut) { |
| 35 | $shortcut = Shortcut::find($shortcut->id); |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | if ($data['shortcut_id'] != 0000) { |
| 40 | $charactersTyped = strlen(strip_tags($shortcut->text)); |
| 41 | $shortcutCharacters = strlen(strip_tags($shortcut->shortcut)); |
| 42 | } else { |
| 43 | $charactersTyped = $data['value']; |
| 44 | $shortcutCharacters = 4; |
| 45 | } |
| 46 | |
| 47 | // $record = HubspotProperties::where('flymsg_id', $data['user_id'])->get(); |
| 48 | |
| 49 | $charactersSaved = $charactersTyped > $shortcutCharacters ? $charactersTyped - $shortcutCharacters : $charactersTyped; |
| 50 | $timeSaved = $this->statisticsService->getTimeSaved($user, $charactersSaved); |
| 51 | $costSaved = $this->statisticsService->getCostSaved($user, $timeSaved, now()); |
| 52 | |
| 53 | // Update Usage data on the shortcut model for quick access. |
| 54 | if ($data['shortcut_id'] != 0000) { |
| 55 | |
| 56 | // This is being done already in the TrackingExtensionController line 117. |
| 57 | // Don't uncomment this to avoid duplicate entries. |
| 58 | // Left here for demo only |
| 59 | if (isset($shortcut->flycutUsage_count)) { |
| 60 | // $shortcut->flycutUsage_count = $shortcut->flycutUsage_count + 1; |
| 61 | } else { |
| 62 | $shortcut->flycutUsage_count = 1; |
| 63 | } |
| 64 | |
| 65 | if (isset($shortcut->flycutUsage_time_saved)) { |
| 66 | $shortcut->flycutUsage_time_saved = $shortcut->flycutUsage_time_saved + $timeSaved; |
| 67 | } else { |
| 68 | $shortcut->flycutUsage_time_saved = $timeSaved; |
| 69 | } |
| 70 | |
| 71 | if (isset($shortcut->flycutUsage_cost_saved)) { |
| 72 | $shortcut->flycutUsage_cost_saved = $shortcut->flycutUsage_cost_saved + $costSaved; |
| 73 | } else { |
| 74 | $shortcut->flycutUsage_cost_saved = $costSaved; |
| 75 | } |
| 76 | |
| 77 | // Update last_deployed_date for recently used. |
| 78 | $shortcut->last_deployed_date = Carbon::now()->toIso8601String(); |
| 79 | $shortcut->save(); |
| 80 | } |
| 81 | |
| 82 | $user->flycutUsage()->create([ |
| 83 | 'shortcut_id' => $data['shortcut_id'] == 0000 ? 1 : $data['shortcut_id'], |
| 84 | 'characters_typed' => $charactersTyped, |
| 85 | 'shortcut_characters' => $shortcutCharacters, |
| 86 | 'characters_saved' => $charactersSaved, |
| 87 | 'time_saved' => $timeSaved, |
| 88 | 'cost_saved' => $costSaved, |
| 89 | 'executed_at_raw' => $data['executed_at'], |
| 90 | 'executed_at' => $this->getSiteNameFromUrl($data['executed_at']), |
| 91 | ]); |
| 92 | |
| 93 | if ($data['tag'] === 'total_char_by_a_FlyCut' && $browser_type) { |
| 94 | $this->updateSync($user->id, $browser_type, $data); |
| 95 | } |
| 96 | |
| 97 | $siteName = $this->getSiteNameFromUrl($data['executed_at']); |
| 98 | $monthYear = now()->format('M Y'); |
| 99 | |
| 100 | // Update or create a FlymsgDeployment record with the given site and date. |
| 101 | $record = FlymsgDeploymentRecord::where('site_name', $siteName)->where('month_year', $monthYear)->first(); |
| 102 | |
| 103 | if ($record) { |
| 104 | //We're going back to the db to avoid concurrency issues |
| 105 | FlymsgDeploymentRecord::firstWhere('_id', $record->id)->increment('usage_count'); |
| 106 | } else { |
| 107 | FlymsgDeploymentRecord::create([ |
| 108 | 'site_name' => $siteName, |
| 109 | 'month_year' => $monthYear, |
| 110 | 'usage_count' => 1, |
| 111 | ]); |
| 112 | } |
| 113 | } catch (Exception $e) { |
| 114 | return response()->json(['data' => [], 'message' => $e->getMessage(), 'error' => 1], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Gets the name of the site from a given URL. |
| 120 | * |
| 121 | * @param string $url The URL to extract the site name from. |
| 122 | * |
| 123 | * @return string The name of the site without the "www." prefix, or an empty string if the URL is empty. |
| 124 | */ |
| 125 | private function getSiteNameFromUrl(string $url) |
| 126 | { |
| 127 | if (empty($url)) { |
| 128 | return ''; |
| 129 | } |
| 130 | |
| 131 | $domain = parse_url($url, PHP_URL_HOST); |
| 132 | |
| 133 | return preg_replace('/^www\./', '', $domain); |
| 134 | } |
| 135 | |
| 136 | protected function updateSync($userId, $browser_type, $data) |
| 137 | { |
| 138 | // Start of Updating propreties. |
| 139 | $user = User::firstWhere('_id', $userId); |
| 140 | $userInfo = UserInfo::where('email', $user->email)->first(); |
| 141 | if ($userInfo) { |
| 142 | $userInfo->email_used_for_login = $user->email; |
| 143 | } |
| 144 | |
| 145 | $browser = strtolower($browser_type); |
| 146 | |
| 147 | if ($userInfo) { |
| 148 | if ($userInfo->{"flymsg_{$browser}_extension_installed"} !== 'Yes' || $userInfo->{"flymsg_{$browser}_extension_uninstalled"} === 'Yes') { |
| 149 | $userInfoService = new UserInfoService(new StatisticsService()); |
| 150 | |
| 151 | $properties = $userInfoService->addExtension($userInfo, $browser, now()); |
| 152 | |
| 153 | $userInfo->fill($properties); |
| 154 | } else { |
| 155 | $userInfo->signed_into_flymsg_extension = 'Yes'; |
| 156 | $userInfo->last_browser_used = ucfirst(strtolower($browser_type)); |
| 157 | } |
| 158 | |
| 159 | if ($data && $data["extension_tracking_headers"]) { |
| 160 | if ($data["extension_tracking_headers"]["flymsg_extension_version_for_chrome"]) { |
| 161 | $userInfo->flymsg_extension_version_for_chrome = $data["extension_tracking_headers"]["flymsg_extension_version_for_chrome"]; |
| 162 | } |
| 163 | |
| 164 | if ($data["extension_tracking_headers"]["flymsg_extension_version_for_edge"]) { |
| 165 | $userInfo->flymsg_extension_version_for_edge = $data["extension_tracking_headers"]["flymsg_extension_version_for_edge"]; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | $userInfo->save(); |
| 170 | } |
| 171 | } |
| 172 | } |