<?php

namespace App\Traits;

use App\Mail\UserSelfActivated;
use App\User;
use App\UserActivity;
use Illuminate\Support\Facades\Mail;

trait ActivatesUsers
{
    private function logActivatedActivity(User $createdUser, $activityDateTime = null)
    {
        if (is_null($activityDateTime))
        {
            $activityDateTime = new \DateTime();
        }

        $userActivity = new UserActivity();
        $userActivity->user_id = $createdUser->id;
        $userActivity->activity_at = $activityDateTime;
        $userActivity->type = 'user.created';
        $userActivity->save();
    }

    private function sendUserActivatedEmails(User $createdUser)
    {
        $adminUsers = User::where('is_admin', true)->get();

        /** @var User $adminUser */
        foreach ($adminUsers as $adminUser)
        {
            Mail::to($adminUser)->send(new UserSelfActivated($adminUser, $createdUser));
        }
    }
}