Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
26.32% |
20 / 76 |
|
41.67% |
5 / 12 |
CRAP | |
0.00% |
0 / 1 |
| GoogleController | |
26.32% |
20 / 76 |
|
41.67% |
5 / 12 |
180.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAuthUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| postLogin | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
12 | |||
| getMeGoogle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getCustomers | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getConversionActionCategories | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getConversionActionTypes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getConversionActionStatuses | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getCampaigns | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| getConversionActions | |
28.57% |
2 / 7 |
|
0.00% |
0 / 1 |
3.46 | |||
| postConversionAction | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| __validateConversionActionRequest | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace App\Http\Controllers\api; |
| 3 | |
| 4 | use Illuminate\Http\Request; |
| 5 | use Illuminate\Http\JsonResponse; |
| 6 | use App\Http\Controllers\api\BaseController as BaseController; |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Support\Facades\Auth; |
| 9 | use Validator; |
| 10 | |
| 11 | use App\Traits\GoogleTrait; |
| 12 | |
| 13 | class GoogleController extends BaseController |
| 14 | { |
| 15 | use GoogleTrait; |
| 16 | |
| 17 | protected $googleAds; |
| 18 | |
| 19 | public function __construct(\App\Libs\GoogleAds $ga) { |
| 20 | $this->googleAds = $ga; |
| 21 | |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Return the url of the google auth. |
| 26 | * FE should call this and then direct to this url. |
| 27 | * |
| 28 | * @return JsonResponse |
| 29 | */ |
| 30 | public function getAuthUrl(Request $request):JsonResponse |
| 31 | { |
| 32 | $client = $this->getClient(); |
| 33 | return $this->sendResponse(200, ['url' => stripslashes($client->createAuthUrl())]); |
| 34 | } // getAuthUrl |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Login and register |
| 39 | * Gets registration data by calling google Oauth2 service |
| 40 | * |
| 41 | * @return JsonResponse |
| 42 | */ |
| 43 | public function postLogin(Request $request):JsonResponse |
| 44 | { |
| 45 | $normalUserRoleId = 2; |
| 46 | |
| 47 | /** |
| 48 | * Get authcode from the query string |
| 49 | */ |
| 50 | $authCode = urldecode($request->input('auth_code')); |
| 51 | |
| 52 | /** |
| 53 | * Google client |
| 54 | */ |
| 55 | $client = $this->getClient(); |
| 56 | |
| 57 | /** |
| 58 | * Exchange auth code for access token |
| 59 | * Note: if we set 'access type' to 'force' and our access is 'offline', we get a refresh token. we want that. |
| 60 | */ |
| 61 | $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); |
| 62 | |
| 63 | /** |
| 64 | * Set the access token with google. nb json |
| 65 | */ |
| 66 | $client->setAccessToken(json_encode($accessToken)); |
| 67 | |
| 68 | /** |
| 69 | * Get user's data from google |
| 70 | */ |
| 71 | $service = new \Google\Service\Oauth2($client); |
| 72 | $userFromGoogle = $service->userinfo->get(); |
| 73 | |
| 74 | /** |
| 75 | * Select user if already exists |
| 76 | */ |
| 77 | $user = User::where('provider_name', '=', 'google') |
| 78 | ->where('provider_id', '=', $userFromGoogle->id) |
| 79 | ->first(); |
| 80 | |
| 81 | /** |
| 82 | * Create user if not already exists |
| 83 | * Save google access token |
| 84 | * HTTP 403 |
| 85 | */ |
| 86 | if (!$user) { |
| 87 | try { |
| 88 | $user = User::create([ |
| 89 | 'provider_id' => $userFromGoogle->id, |
| 90 | 'provider_name' => 'google', |
| 91 | 'google_access_token_json' => json_encode($accessToken), |
| 92 | 'name' => $userFromGoogle->name, |
| 93 | 'email' => $userFromGoogle->email, |
| 94 | 'role_id' => $normalUserRoleId, |
| 95 | //'avatar' => $providerUser->picture, // in case you have an avatar and want to use google's |
| 96 | ]); |
| 97 | } catch (\Illuminate\Database\QueryException $e) { |
| 98 | return $this->sendError(403, "Error logging in"); |
| 99 | } |
| 100 | } |
| 101 | /** |
| 102 | * Save new access token for existing user |
| 103 | */ |
| 104 | else { |
| 105 | $user->google_access_token_json = json_encode($accessToken); |
| 106 | $user->save(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Log in and return token |
| 111 | * HTTP 201 |
| 112 | */ |
| 113 | $success['token'] = $user->createToken("Google")->accessToken; |
| 114 | $success['name'] = $user->name; |
| 115 | $success['email'] = $user->email; |
| 116 | $success['user_id'] = $user->id; |
| 117 | return $this->sendResponse(201, $success); |
| 118 | } // postLogin |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * Gets session user's google oauth data |
| 123 | * |
| 124 | * @return JsonResponse |
| 125 | */ |
| 126 | public function getMeGoogle(Request $retreiving):JsonResponse |
| 127 | { |
| 128 | $client = $this->getUserClient(); |
| 129 | $service = new \Google\Service\Oauth2($client); |
| 130 | $userFromGoogle = $service->userinfo->get(); |
| 131 | return $this->sendResponse(200, $userFromGoogle); |
| 132 | } // getMeGoogle |
| 133 | |
| 134 | |
| 135 | /** |
| 136 | * Gets all the customers for a google ads account |
| 137 | * |
| 138 | * @return JsonResponse |
| 139 | */ |
| 140 | public function getCustomers(Request $request):JsonResponse |
| 141 | { |
| 142 | /** |
| 143 | * Get customers |
| 144 | * @see app.Traits.GoogleTrait |
| 145 | */ |
| 146 | $customers = $this->googleAds->getGoogleAdsCustomers(); |
| 147 | |
| 148 | /** |
| 149 | * No customers |
| 150 | * HTTP 404 |
| 151 | */ |
| 152 | if (count($customers) == 0) { |
| 153 | return $this->sendError(404, "No accessible customers"); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * HTTP 200 |
| 158 | */ |
| 159 | return $this->sendResponse(200, $customers); |
| 160 | } // getCustomers |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Returns an array of conversion action categories suitable for menus |
| 165 | * |
| 166 | * @return JsonResponse |
| 167 | * @see http://swagger-humanverify.fruitbat.systems/#post-/api/conversions/actions/categories |
| 168 | */ |
| 169 | public function getConversionActionCategories(Request $request):JsonResponse |
| 170 | { |
| 171 | $returnArray = $this->googleAds->getGoogleAdsConversionActionCategories(); |
| 172 | |
| 173 | /** |
| 174 | * HTTP 200 |
| 175 | */ |
| 176 | return $this->sendResponse(200, $returnArray); |
| 177 | } // getConversionActionCategories |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Returns an array of conversion action types suitable for menus |
| 182 | * |
| 183 | * @return JsonResponse |
| 184 | * @see http://swagger-humanverify.fruitbat.systems/#post-/api/conversions/actions/types |
| 185 | */ |
| 186 | public function getConversionActionTypes(Request $request):JsonResponse |
| 187 | { |
| 188 | $returnArray = $this->googleAds->getGoogleAdsConversionActionTypes(); |
| 189 | |
| 190 | /** |
| 191 | * HTTP 200 |
| 192 | */ |
| 193 | return $this->sendResponse(200, $returnArray); |
| 194 | } // getConversionActionTypes |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * Returns an array of conversion action statuses suitable for menus |
| 199 | * |
| 200 | * @return JsonResponse |
| 201 | * @see http://swagger-humanverify.fruitbat.systems/#post-/api/conversions/actions/statuses |
| 202 | */ |
| 203 | public function getConversionActionStatuses(Request $request):JsonResponse |
| 204 | { |
| 205 | $returnArray = $this->googleAds->getGoogleAdsConversionActionStatuses(); |
| 206 | |
| 207 | /** |
| 208 | * HTTP 200 |
| 209 | */ |
| 210 | return $this->sendResponse(200, $returnArray); |
| 211 | } // getConversionActionTypes |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Get all campaigns for a given customer |
| 216 | * |
| 217 | * @param String $id The id of the customer |
| 218 | * @return JsonResponse |
| 219 | * @see |
| 220 | */ |
| 221 | public function getCampaigns(Request $request, String $id):JsonResponse |
| 222 | { |
| 223 | try { |
| 224 | $campaigns = $this->googleAds->getGoogleAdsCampaigns($id); |
| 225 | } catch (\Exception $e) { |
| 226 | $googleMessage = $e->getMessage(); |
| 227 | $googleMessage = json_decode($googleMessage); |
| 228 | $googleMessage = $googleMessage->details[0]->errors[0]->message; |
| 229 | return $this->sendError(500, "Could not select campaigns", $googleMessage); |
| 230 | } |
| 231 | return $this->sendResponse(200, $campaigns); |
| 232 | } // getCampaigns |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Gets all the conversion actions for the customer |
| 237 | * |
| 238 | * @param String $customerId The id of the customer |
| 239 | * @return JsonResponse |
| 240 | * @see |
| 241 | */ |
| 242 | public function getConversionActions(Request $request, String $customerId):JsonResponse |
| 243 | { |
| 244 | try { |
| 245 | $conversionActions = $this->googleAds->getGoogleAdsConversionActions($customerId); |
| 246 | } catch (\Exception $e) { |
| 247 | $googleMessage = $e->getMessage(); |
| 248 | $googleMessage = json_decode($googleMessage); |
| 249 | $googleMessage = $googleMessage->details[0]->errors[0]->message; |
| 250 | return $this->sendError(500, "Could not get conversion actions", $googleMessage); |
| 251 | } |
| 252 | return $this->sendResponse(200, $conversionActions); |
| 253 | } // getConversionActions |
| 254 | |
| 255 | |
| 256 | /** |
| 257 | * Creates a conversion action for the customer |
| 258 | * |
| 259 | * @param String $customerId The id of the customer |
| 260 | * @return JsonResponse |
| 261 | * @see |
| 262 | */ |
| 263 | public function postConversionAction(Request $request, String $customerId):JsonResponse |
| 264 | { |
| 265 | |
| 266 | /** |
| 267 | * Validate company invite request body |
| 268 | * HTTP 400 |
| 269 | */ |
| 270 | $validationResult = $this->__validateConversionActionRequest($request); |
| 271 | if (!is_bool($validationResult)) { |
| 272 | return $validationResult; |
| 273 | } |
| 274 | |
| 275 | $name = $request->name; |
| 276 | $category = $request->category; |
| 277 | $type = $request->type; |
| 278 | $status = $request->status; |
| 279 | $defaultValue = $request->default_value; |
| 280 | $viewThroughLookbackWindowDays = $request->view_through_lookback_window_days; |
| 281 | |
| 282 | /** |
| 283 | * Create the conversion action |
| 284 | */ |
| 285 | try { |
| 286 | $this->createGoogleAdsConversionAction($customerId, $name, $category, $type, $status, $defaultValue, $viewThroughLookbackWindowDays); |
| 287 | } catch (\Exception $e) { |
| 288 | $googleMessage = $e->getMessage(); |
| 289 | $googleMessage = json_decode($googleMessage); |
| 290 | $googleMessage = $googleMessage->details[0]->errors[0]->message; |
| 291 | return $this->sendError(500, "Could not create conversion action", $googleMessage); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * HTTP 201 |
| 296 | */ |
| 297 | return $this->sendResponse(201, 'created'); |
| 298 | |
| 299 | } // postConversionAction |
| 300 | |
| 301 | |
| 302 | protected function __validateConversionActionRequest(Request $request) // @phpstan-ignore-line |
| 303 | { |
| 304 | $validationRules = [ |
| 305 | 'name' => 'required|string|max:255', |
| 306 | 'category' => 'integer|min:1', |
| 307 | 'type' => 'integer|min:1', |
| 308 | 'status' => 'integer|min:1', |
| 309 | 'view_through_lookback_window_days' => 'integer|min:1', |
| 310 | 'default_value' => 'required|numeric|between:0,99999.99', |
| 311 | ]; |
| 312 | $validationResult = $this->__validateJson($validationRules, $request); |
| 313 | if (is_array($validationResult)) { |
| 314 | return $this->sendError(400, $validationResult, "Conversion action data is not valid"); |
| 315 | } else { |
| 316 | return true; |
| 317 | } |
| 318 | } // __validateConversionActionRequest |
| 319 | |
| 320 | |
| 321 | |
| 322 | } |