Advertisement
Guest User

sdfsdfsd

a guest
Nov 17th, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2.  
  3. namespace HL\Community;
  4.  
  5. use App\Events\UserReceiveCredits;
  6. use App\Events\UserSendCredits;
  7. use HL\Auth\User;
  8. use HL\Auth\UserComment;
  9. use HL\Auth\UserGalleryItem;
  10. use HL\Auth\UserProfile;
  11. use HL\Auth\UserRating;
  12. use HL\Auth\UserRepository;
  13. use HL\Stream\PrivateTemplate;
  14. use \Illuminate\Contracts\Events\Dispatcher;
  15.  
  16. class CommunityService {
  17.  
  18.     private $UserRepository;
  19.     private $Events;
  20.  
  21.     public function __construct( UserRepository $userRepository , Dispatcher $events ) {
  22.         $this->UserRepository = $userRepository;
  23.         $this->Events = $events;
  24.     }
  25.  
  26.     public function addComment( User $sender , User $target , $content ) {
  27.  
  28.         $comment = new UserComment();
  29.         $comment->commenter_id = $sender->id;
  30.         $comment->user_id = $target->id;
  31.         $comment->content = nl2br( htmlspecialchars( $content ) );
  32.         $comment->save();
  33.  
  34.         return $comment;
  35.  
  36.     }
  37.  
  38.     public function addPhoto( User $user , $fileName , $description ) {
  39.  
  40.         $galleryItem = new UserGalleryItem();
  41.         $galleryItem->image = $fileName;
  42.         $galleryItem->description = $description;
  43.         $user->gallery()->save( $galleryItem );
  44.  
  45.         return $galleryItem;
  46.  
  47.     }
  48.  
  49.     public function removePhoto( User $user , $galleryItemId ) {
  50.  
  51.         $galleryItem = $user->gallery()->find( $galleryItemId );
  52.         if ( $galleryItem ) {
  53.             $galleryItem->delete();
  54.         } else {
  55.             throw new \Exception( 'Invalid gallery item id.' );
  56.         }
  57.  
  58.     }
  59.  
  60.     public function transferCredits( User $sender , User $receiver , $amount ) {
  61.  
  62.         if ( $sender->credits < $amount ) {
  63.             return false;
  64.         }
  65.  
  66.         $sender->credits -= $amount;
  67.         $sender->save();
  68.  
  69.         $receiver->credits += $amount;
  70.         $receiver->save();
  71.  
  72.         $this->Events->trigger( new UserSendCredits( $sender , $receiver , $amount ) );
  73.         $this->Events->trigger( new UserReceiveCredits( $receiver , $sender , $amount ) );
  74.  
  75.         return true;
  76.  
  77.     }
  78.  
  79.     public function updateProfile( User $user , array $values ) {
  80.  
  81.         $profile = $user->profile()->first();
  82.  
  83.         if ( !$profile ) {
  84.             $profile = new UserProfile();
  85.             $profile->user_id = $user->id;
  86.         }
  87.  
  88.         foreach( [ 'name' , 'sex' , 'age' , 'eye_color' , 'breast_size' , 'height' , 'weight' , 'description' ] as $key ) {
  89.             if ( array_has( $values , $key ) ) {
  90.                 if ( $key == 'description' ) {
  91.                     $profile[ $key ] = nl2br( htmlspecialchars( $values[$key] ) );
  92.                 } else {
  93.                     $profile[ $key ] = $values[$key];
  94.                 }
  95.             }
  96.         }
  97.  
  98.         $profile->save();
  99.  
  100.     }
  101.  
  102.     public function userSetAvatar( User $user , $avatarName ) {
  103.  
  104.         $user->avatar = $avatarName;
  105.         $user->save();
  106.  
  107.     }
  108.  
  109.     public function rateUser( User $sender , User $target , $rating ) {
  110.  
  111.         if ( $target->ratings()->where( 'rater_id' , '=' , $sender->id )->count() > 0 ) {
  112.             throw new \Exception( 'This user already posted his rating.' );
  113.         }
  114.  
  115.         if ( intval( $rating ) < 0 || intval( $rating ) > 10 ) {
  116.             throw new \Exception( 'Invalid rating.' );
  117.         }
  118.  
  119.         $userRating = new UserRating();
  120.         $userRating->user_id = $target->id;
  121.         $userRating->rater_id = $sender->id;
  122.         $userRating->rating = $rating;
  123.         $userRating->save();
  124.  
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement