Completed
Push — master ( 28284e...eaa9bf )
by
unknown
12:07
created

VendorController::vote_vendor()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 52
Code Lines 32

Duplication

Lines 18
Ratio 34.62 %

Importance

Changes 0
Metric Value
dl 18
loc 52
rs 6.5703
c 0
b 0
f 0
cc 9
eloc 32
nc 36
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\VendorFormRequest;
6
use App\Http\Requests\VendorUpdateFormRequest;
7
use App\Repositories\VendorRepository;
8
use Illuminate\Http\Request;
9
10
class VendorController extends Controller
11
{
12
    /**
13
     * @var VendorRepository
14
     */
15
    protected $vendors;
16
17
    /**
18
     * VendorController constructor.
19
     * @param VendorRepository $vendorRepository
20
     */
21
    public function __construct(VendorRepository $vendorRepository)
22
    {
23
        $this->vendors = $vendorRepository;
24
    }
25
26
    public function index()
27
    {
28
        $vendors = $this->vendors->getPublic($paginate = 9);
29
        
30
        return view('vendors.index', compact('vendors'));
31
    }
32
33
    /**
34
     * Create vendor.
35
     *
36
     * @param VendorFormRequest $request
37
     * @return \Illuminate\Http\RedirectResponse
38
     */
39
    public function postCreate(VendorFormRequest $request)
40
    {
41
        $vendor = $this->vendors->create($request->all());
42
43
        return redirect()->route('view_vendor', ['vendor' => $vendor->slug]);
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<App\Vendor>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
    }
45
46
    /**
47
     * Create form for vendor.
48
     *
49
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
50
     */
51
    public function getCreate()
52
    {
53
        return view('vendors.create');
54
    }
55
56
    /**
57
     * Show vendor.
58
     * 
59
     * @param $vendor
60
     * @return mixed
61
     */
62
    public function show($vendor)
63
    {
64
        return view('vendors.show')->withItem($vendor);
65
    }
66
67
    /**
68
     * Edit form for vendor.
69
     *
70
     * @param $vendor
71
     * @return mixed
72
     */
73
    public function edit($vendor)
74
    {
75
        return view('vendors.edit')->withItem($vendor);
76
    }
77
78
    /**
79
     * Update vendor.
80
     * 
81
     * @param VendorUpdateFormRequest $request
82
     * @param $vendor
83
     * @return \Illuminate\Http\RedirectResponse
84
     */
85
    public function update(VendorUpdateFormRequest $request, $vendor)
86
    {
87
        $this->vendors->update($vendor, $request->all());
88
89
        return redirect()->route('view_vendor', ['slug' => $vendor->slug]);
90
    }
91
92
    public function vote_vendor(Request $request, $vendor)
93
    {
94
        if($request->get('like_type') == 'like')
95
        {
96 View Code Duplication
            if($vendor->wasLiked('like')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
                $vendor->unlike('like');
98
            } else {
99
                if($vendor->wasLiked('dislike'))
100
                    $vendor->unlike('dislike');
101
102
                $vendor->like('like');
103
            }
104 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
            if($vendor->wasLiked('dislike')) {
106
                $vendor->unlike('dislike');
107
            } else {
108
                if($vendor->wasLiked('like'))
109
                    $vendor->unlike('like');
110
111
                $vendor->like('dislike');
112
            }
113
        }
114
        if($vendor->wasLiked('like')){
115
            
116
            $was_liked = 'like';
117
        }
118
        elseif($vendor->wasLiked('dislike')) {
119
120
            $was_liked = 'unlike';
121
        }
122
        else{
123
            $was_liked = "not_liked"; 
124
        }
125
126
       if($vendor->likes()->count())
127
        {
128
            $positive_like_percent = 
129
            ($vendor->likes()->count() - $vendor->getLikes('dislike')->count()) / $vendor->likes()->count() * 100;
130
        }
131
        else {
132
            $positive_like_percent = 0; 
133
        }
134
135
136
        return json_encode([
137
            'likes' => $vendor->getLikes('like')->count(),
138
            'dislikes' => $vendor->getLikes('dislike')->count(),
139
            'likes_count' => $vendor->likes()->count(),
140
            'likes_percent' => $positive_like_percent,
141
            'was_liked' => $was_liked
142
        ]);
143
    }
144
}