Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
64.29% covered (warning)
64.29%
27 / 42
ProfileController
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
20.70
64.29% covered (warning)
64.29%
27 / 42
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 show
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 editUser
0.00% covered (danger)
0.00%
0 / 1
19.39
54.55% covered (warning)
54.55%
18 / 33
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use App\Image;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Exception;
class ProfileController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    /**
     * Shows the User profile for a given id.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $user = User::find($id);
        $skills = DB::select("SELECT skillName FROM skill, skill_user WHERE skill.id=skill_user.idSkill AND skill_user.idUser = ?", [$user->id]);
        $user->skills=$skills;
        $images = DB::table('image')->where('idusers', $id)->pluck('source');
        if (sizeof($images) == 0) {
            $images = ["default.png"];
        }
        return view('pages.profile', ['user' => $user, 'image' => $images[0]]);
    }
    /**
      * Edits an user profile
      * @param Request $request
      * @param int $id
      * @return redirect to profile
      */
    public function editUser(Request $request, $id)
    {
        if (Auth::user()->id != $id) {
            return redirect('/home');
        }
        $validator = Validator::make($request->all(), [
            'name' => 'nullable|string|max:255',
            'email' => 'nullable|string|email|max:255|unique:users',
            'idfaculty' => 'nullable|integer',
        ]);
        if ($validator->fails()) {
            return redirect()
                ->route('profile', ['id' => Auth::user()->id])
                ->withErrors($validator)
                ->withInput();
        }
        $input = $request->all();
        try {
            if ($input['name'] !== null) {
                DB::update('update users set name = ? where id = ?', [$input['name'], $id]);
            }
            if ($input['email'] !== null) {
                DB::update('update users set email = ? where id = ?', [$input['email'], $id]);
            }
            if ($input['idfaculty'] !== null) {
                DB::update('update users set idfaculty = ? where id = ?', [$input['idfaculty'], $id]);
            }
            if ($input['phone'] !== null) {
                DB::update('update users set phone = ? where id = ?', [$input['phone'], $id]);
            }
            $image = $request->file('image');
            if ($image !== null) {
                $input['imagename'] = time().$image->getClientOriginalName();
                $image->move('img', $input['imagename']);
                if (sizeof(DB::select('SELECT * FROM image WHERE idusers = ?', [$id])) > 0) {
                    DB::update('UPDATE image SET source = ? WHERE idusers = ?', [$input['imagename'], $id]);
                } else {
                    DB::insert('INSERT INTO image (source,idusers) VALUES(?,?)', [$input['imagename'], $id]);
                }
            }
        } catch (QueryException $qe) {
            $errors = new MessageBag();
            $errors->add('An error ocurred', "There was a problem editing your information. Try Again!");
            $this->warn($qe);
            return redirect()
                ->route('profile', ['id' => Auth::user()->id])
                ->withErrors($errors);
        }
        return redirect()->route('profile', ['id' => Auth::user()->id]);
    }
}