NewsletterController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 26
loc 26
rs 10
wmc 3
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A subscribe() 18 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http\Controllers\Front\Api;
4
5
use Newsletter;
6
use App\Http\Requests\Front\Api\NewsletterSubscriptionRequest;
7
8 View Code Duplication
class NewsletterController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
9
{
10
    /**
11
     * @param \App\Http\Requests\Front\NewsletterSubscriptionRequest $request
12
     *
13
     * @return \Illuminate\Http\JsonResponse
14
     */
15
    public function subscribe(NewsletterSubscriptionRequest $request)
16
    {
17
        $email = strtolower($request->get('email'));
18
19
        if (Newsletter::hasMember($email)) {
20
            return $this->respond(['message' => fragment('newsletter.subscription.result.alreadySubscribed'), 'type' => 'info']);
21
        }
22
23
        $result = Newsletter::subscribe($email);
24
25
        if (! $result) {
26
            return $this->respondWithBadRequest(['message' => fragment('newsletter.subscription.result.error'), 'type' => 'error']);
0 ignored issues
show
Documentation introduced by
array('message' => fragm...r'), 'type' => 'error') is of type array<string,string,{"me...ring","type":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
        }
28
29
        activity()->log("{$email} schreef zich in op de nieuwsbrief");
30
31
        return $this->respond(['message' => fragment('newsletter.subscription.result.ok'), 'type' => 'success']);
32
    }
33
}
34