ctl/socialmapfavorites

Basic Packaging for Favoriting and Unfavoriting users

1.2.0 2016-12-11 21:11 UTC

README

Latest Stable Version Software License Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Unstable Version

A PHP Package for Favoriting and Unfavoriting Authenticated Users

Currently only supported for Laravel 5.2

Install

Via Composer

$ composer require ctl/socialmapfavorites

Installation

$ php artisan vendor:publish

Add the service provider to the 'providers' array in config/app.php

CTL\SocialMapFavorites\SocialMapFavoritesServiceProvider::class,

Navigate to App/Jobs/FavAUser. Replace use CTL\SocialMapFavorites\Jobs\Job; with use App\Jobs\Job;

Repeat above with unFavAUser

Navigage to App\Users. Add ActionableTrait like so....

<?php

namespace App;

use Core\Users\ActionableTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, ActionableTrait;

    /**
     * The database table used by the model.

Add "Core\\": "Core/" to your project composer.json. It should look like this...

"psr-4": {
            "App\\": "app/",
            "Core\\": "Core/"
        }

run composer dump-autoload if changes does not take effect.

Change the namespace of Core\Users\ActionableTrait and Core\Users\UsersOrigin

Finally, run....

$ php artisan make:migration CreateFavoritesTable

To create a favs table

Your Migration should look like

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFavoritesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('favs', function(Blueprint $table){
            $table->increments('id');
            $table->integer('favoritee_id')->index();
            $table->integer('favorited_id')->index();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('favs', function(Blueprint $table){
            Schema::drop('favs');
        });
    }
}

Extended Installation

Within blade files..

    @if (!$VariableName->checkFavorited() )
        <div class="btn-group">
          {!! Form::open(['action'=>'ControllerName@store']) !!}
          {!! Form::hidden('userIDToFav', $VariableName->id) !!}

            <button type="submit" class="btn btn-success">Add Fav</button>
          {!! Form::close() !!}
        </div>
    @else
        {!! Form::open(['method' => 'DELETE', 'action' => ['ControllerName@destroy', $VariableName->id] ]) !!}
        {!! Form::hidden('userIDToUnFav', $VariableName->id) !!}
      <div class="btn-group">
        <button type="submit" class="btn btn-primary"><span>Favorited</span></button>
        {!! Form::close() !!}
      </div>
    @endif

Within your controller

    /**
     * Favorite A User
     *
     */
    public function store(Request $request){
      $base = $request->input('userIDToFav');
      $this->dispatch(new FavAUser(\Auth::id(), $base));

      return back();
    }

    /**
     * UnFavorite a user
     *
     */
    public function destroy(Request $request){
      $base = $request->input('userIDToUnFav');
      $this->dispatch(new UnFavAUser(\Auth::id(), $base));

      return back();
    }

For Email Usage

Within the controller

    /**
     * Fav A User
     *
     * @return Response
     */
    public function store(User $user, Request $request)
    {
        $base = array_add($request, 'userID', Auth::id() );
        $clear = $this->dispatchFrom(FavAUserCommand::class, $base);

        // For emailing User Fav
        $findingUserObject = $user->find($request->input('userIDToFav'));
        $this->mail->sendFavAUserNotificationEmail($findingUser, $request->input('userFaved'), Auth::user()->username);


        session()->flash('success_message','You are now following');
        return back();
    }

For $this->mail->sendFavAUserNotificationEmail();

You would need to

    protected $mail;
    protected $usersOrigin;


    /**
    * Class Constructor
    */
    public function __construct(UsersOrigin $usersOrigin, FavAUserMail $mail){

      $this->mail = $mail;
      $this->usersOrigin = $usersOrigin;
    }

Dont forget to add use Core\Users\Mail\FavAUserMail; and use Core\Users\UsersOrigin; at the top of your controller class.

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

License

Eclipse Public License (EPL v1.0). Please see License for more information.