<?php

namespace App\Model\Services;



use App\Model\Entities\Security\User;
use App\Model\Entities\Security\UserCredentials;

class UserCredentialsService extends BaseService
{

    /**
     * @param $id
     * @return null|User
     */
    public function findCredentialsById($id)
    {
        return $this->getRepository(UserCredentials::class)->find($id);
    }

    /**
     * @param $email
     * @return UserCredentials|null
     */
    public function findCredentialsByEmail($email)
    {
        $criteria = array('email' => $email);

        return $this->getRepository(UserCredentials::class)->findOneBy($criteria);
    }

    public function getUserCredentials(User $user)
    {
        $criteria = array('user' => $user);

        return $this->getRepository(UserCredentials::class)->findOneBy($criteria);
    }

    /**
     * @param User $user
     * @param $email
     * @param $password
     * @return UserCredentials
     */
    public function createCredentials(User $user, $email, $password)
    {
        $credentials  = new UserCredentials();
        $credentials->setUser($user);
        $credentials->setEmail($email);
        $credentials->setPassword($password);

        $this->persist($credentials);
        return $credentials;
    }

    /**
     * @param UserCredentials $credentials
     * @param $password
     * @return UserCredentials
     */
    public function rehashPassword(UserCredentials $credentials, $password)
    {
        $credentials->setPassword($password);

        return $credentials;
    }
}