SpotsController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 49
c 5
b 0
f 0
dl 0
loc 101
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 7 1
A create() 0 12 1
B store() 0 57 6
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Spot;
6
use App\Country;
7
use App\Mail\SpotSubmitted;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Mail;
11
use Illuminate\Support\Facades\Cache;
12
use Intervention\Image\Facades\Image;
13
use Illuminate\Support\Facades\Storage;
14
15
class SpotsController extends Controller
16
{
17
    /**
18
     * Show the form for creating a new Spot.
19
     *
20
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
21
     */
22
    public function create()
23
    {
24
        $minutes = 525600; // One year
25
        
26
        $countries = Cache::remember('countries', $minutes, function () {
27
            return Country::whereIn('continent_id', [2, 3, 6, 7])
28
            ->select('id', 'name_pt')
29
            ->orderBy('name_pt', 'asc')
30
            ->get();
31
        });
32
33
        return view('spots.create', compact('countries'));
34
    }
35
36
    /**
37
     * Store a newly created Spot in storage.
38
     *
39
     * @param Request $request
40
     * @return \Illuminate\Http\RedirectResponse
41
     */
42
    public function store(Request $request)
43
    {
44
        $request->validate([
45
            'name' => 'required|string|max:60',
46
            'city' => 'required|string|max:35',
47
            'country' => 'required|exists:countries,id',
48
            'address' => 'required|string|nullable|max:255',
49
            'phone-number' => 'string|nullable|max:20',
50
            'email' => 'email|nullable',
51
            'website' => 'url|nullable',
52
            'image' => 'image|nullable|max:2000',
53
        ]);
54
55
        $image = $request->file('image');
56
        $imagePath = null;
57
58
        if ($image) {
59
            $imagePath = $image->hashName('spots');
60
61
            $img = Image::make($image);
62
            $img->fit(675, 450);
63
64
            Storage::put($imagePath, (string) $img->encode());
65
        }
66
67
        $name = $request->get('name');
68
69
        $slug = str_slug($name);
70
        $slugIndex = 0;
71
72
        // If slug already in use, add an index to the slug
73
        while (Spot::where('slug', $slug)->count()) {
74
            $slugIndex++;
75
            $slug = "$slug-$slugIndex";
76
        }
77
78
        $spot = Spot::create([
79
            'name' => $name,
80
            'slug' => $slug,
81
            'country_id' => $request->get('country'),
82
            'city' => $request->get('city'),
83
            'address' => $request->get('address'),
84
            'email' => $request->get('email'),
85
            'phone_number' => $request->get('phone-number'),
86
            'website' => $request->get('website'),
87
            'image' => $imagePath,
88
        ]);
89
90
        $recipientAddress = config('mail.to')['address'];
91
92
        if ($recipientAddress && (config('mail.status') || app()->environment(['production']))) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        if ($recipientAddress && (config('mail.status') || app()->/** @scrutinizer ignore-call */ environment(['production']))) {
Loading history...
93
            // Send e-mail to admin alerting about new spot submission
94
            Mail::to($recipientAddress)->send(new SpotSubmitted($spot));
95
        }
96
97
        return redirect()->route('home')->with('success',
98
            "<p>Spot <strong>$name</strong> submetido com sucesso!</p><p>Será publicado em breve, após revisão por
99
                parte da nossa equipa. Obrigado!</p>");
100
    }
101
102
    /**
103
     * Display the specified Spot.
104
     *
105
     * @param String $countrySlug
106
     * @param String $spotSlug
107
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
108
     */
109
    public function show(String $countrySlug, String $spotSlug)
0 ignored issues
show
Unused Code introduced by
The parameter $countrySlug is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function show(/** @scrutinizer ignore-unused */ String $countrySlug, String $spotSlug)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $spot = Spot::where('slug', $spotSlug)
112
            ->where('is_approved', true)
113
            ->firstOrFail();
114
115
        return view('spots.show', compact('spot'));
116
    }
117
}
118