award = $award; $this->user = $user; } /** * Run the main handler for this award class to determine if * it should be awarded or not. Declared as final to prevent a child * from accidentally overriding and breaking something */ final public function handle(): void { // Check if the params are a JSON object or array $param = $this->award->ref_model_params; if (Utils::isObject($this->award->ref_model_params)) { $param = json_decode($this->award->ref_model_params); } if ($this->check($param)) { $this->addAward(); } } /** * Add the award to this user, if they don't already have it * * @return bool|UserAward */ final protected function addAward() { $w = [ 'user_id' => $this->user->id, 'award_id' => $this->award->id, ]; $found = UserAward::where($w)->count('id'); if ($found > 0) { return true; } // Associate this award to the user now $award = new UserAward($w); try { $award->save(); } catch (\Exception $e) { Log::error( 'Error saving award: '.$e->getMessage(), $e->getTrace() ); return false; } return $award; } }