Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace HL\Community;
- use App\Events\UserReceiveCredits;
- use App\Events\UserSendCredits;
- use HL\Auth\User;
- use HL\Auth\UserComment;
- use HL\Auth\UserGalleryItem;
- use HL\Auth\UserProfile;
- use HL\Auth\UserRating;
- use HL\Auth\UserRepository;
- use HL\Stream\PrivateTemplate;
- use \Illuminate\Contracts\Events\Dispatcher;
- class CommunityService {
- private $UserRepository;
- private $Events;
- public function __construct( UserRepository $userRepository , Dispatcher $events ) {
- $this->UserRepository = $userRepository;
- $this->Events = $events;
- }
- public function addComment( User $sender , User $target , $content ) {
- $comment = new UserComment();
- $comment->commenter_id = $sender->id;
- $comment->user_id = $target->id;
- $comment->content = nl2br( htmlspecialchars( $content ) );
- $comment->save();
- return $comment;
- }
- public function addPhoto( User $user , $fileName , $description ) {
- $galleryItem = new UserGalleryItem();
- $galleryItem->image = $fileName;
- $galleryItem->description = $description;
- $user->gallery()->save( $galleryItem );
- return $galleryItem;
- }
- public function removePhoto( User $user , $galleryItemId ) {
- $galleryItem = $user->gallery()->find( $galleryItemId );
- if ( $galleryItem ) {
- $galleryItem->delete();
- } else {
- throw new \Exception( 'Invalid gallery item id.' );
- }
- }
- public function transferCredits( User $sender , User $receiver , $amount ) {
- if ( $sender->credits < $amount ) {
- return false;
- }
- $sender->credits -= $amount;
- $sender->save();
- $receiver->credits += $amount;
- $receiver->save();
- $this->Events->trigger( new UserSendCredits( $sender , $receiver , $amount ) );
- $this->Events->trigger( new UserReceiveCredits( $receiver , $sender , $amount ) );
- return true;
- }
- public function updateProfile( User $user , array $values ) {
- $profile = $user->profile()->first();
- if ( !$profile ) {
- $profile = new UserProfile();
- $profile->user_id = $user->id;
- }
- foreach( [ 'name' , 'sex' , 'age' , 'eye_color' , 'breast_size' , 'height' , 'weight' , 'description' ] as $key ) {
- if ( array_has( $values , $key ) ) {
- if ( $key == 'description' ) {
- $profile[ $key ] = nl2br( htmlspecialchars( $values[$key] ) );
- } else {
- $profile[ $key ] = $values[$key];
- }
- }
- }
- $profile->save();
- }
- public function userSetAvatar( User $user , $avatarName ) {
- $user->avatar = $avatarName;
- $user->save();
- }
- public function rateUser( User $sender , User $target , $rating ) {
- if ( $target->ratings()->where( 'rater_id' , '=' , $sender->id )->count() > 0 ) {
- throw new \Exception( 'This user already posted his rating.' );
- }
- if ( intval( $rating ) < 0 || intval( $rating ) > 10 ) {
- throw new \Exception( 'Invalid rating.' );
- }
- $userRating = new UserRating();
- $userRating->user_id = $target->id;
- $userRating->rater_id = $sender->id;
- $userRating->rating = $rating;
- $userRating->save();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement