%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/news/app/Repositories/User/
Upload File :
Create Path :
Current File : /var/www/html/news/app/Repositories/User/UsersEloquentRepository.php

<?php

namespace App\Repositories\User;

use App\Models\Media;
use App\Models\User;
use App\Notifications\SendMailNotification;
use App\Providers\MorphableName;
use App\Repositories\Base\BaseEloquentRepository;
use App\Repositories\Media\MediaRepository;
use App\Repositories\Notification\NotificationRepository;
use Illuminate\Container\Container;
use Illuminate\Support\Collection;
use Mockery\CountValidator\Exception;

class UsersEloquentRepository extends BaseEloquentRepository implements UsersRepository
{
    /**
     * @var NotificationRepository
     */
    private $notificationRepository;
    /**
     * @var MediaRepository
     */
    private $mediaRepository;

    /**
     * UsersEloquentRepository constructor.
     * @param Container $container
     * @param Collection $collection
     * @param NotificationRepository $notificationRepository
     * @param MediaRepository $mediaRepository
     */
    public function __construct(Container $container, Collection $collection, NotificationRepository $notificationRepository, MediaRepository $mediaRepository)
    {
        parent::__construct($container, $collection);
        $this->notificationRepository = $notificationRepository;
        $this->mediaRepository = $mediaRepository;
    }

    public function model()
    {
        return User::class;
    }

    /**
     * @param User $user
     * @return User
     */
    public function enableUser(User $user)
    {
        if ($user->is_active) {
            return $user;
        }
        $user->is_active = true;
        $user->save();
        $this->sendEnabledAccountNotification($user);
        return $user;
    }

    /**
     * @param User $user
     * @return User|mixed
     */
    public function disableUser(User $user)
    {
        if (!$user->is_active) {
            return $user;
        }
        $user->is_active = false;
        $user->save();
        $this->sendDisabledAccountNotification($user);
        return $user;
    }

    /**
     * @param User $user
     * @param $file_name
     * @param $real_file_name
     * @return mixed
     */
    public function changeProfilePicture(User $user, $file_name, $real_file_name)
    {
        if ($user->media) {
            $media = $user->media;
            $media->file_name = $file_name;
            $media->real_file_name = $real_file_name;
            $media->save();
        } else {
            $media = $this->mediaRepository->create([
                'mediable_id'    => $user->id,
                'mediable_type'  => MorphableName::forClass($this->model()),
                'uploader_id'    => $user->id,
                'file_name'      => $file_name,
                'real_file_name' => $real_file_name,
                'media_type'     => Media::MEDIA_TYPE_IMAGE_PROFILE
            ]);
        }
        return $media;
    }

    /**
     * @param User $user
     */
    private function sendEnabledAccountNotification(User $user)
    {
        $notification = $this->notificationRepository->getNotificationByCode('account_enabled');
        if (!$notification) {
            throw new Exception("Notification does not exist.");
        }
        if ($notification->send_notification_flag) {
            $user->notify(new SendMailNotification($user, $notification));
        }
    }

    /**
     * @param User $user
     */
    private function sendDisabledAccountNotification(User $user)
    {
        $notification = $this->notificationRepository->getNotificationByCode('account_disabled');
        if (!$notification) {
            throw new Exception("Notification does not exist.");
        }
        if ($notification->send_notification_flag) {
            $user->notify(new SendMailNotification($user, $notification));
        }
    }

}

Zerion Mini Shell 1.0