WebhookController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 10 1
A update() 0 10 1
A destroy() 0 7 1
A __construct() 0 3 1
A all() 0 8 1
A getTenantId() 0 4 3
1
<?php
2
3
namespace Mpociot\CaptainHook\Http;
4
5
use Illuminate\Http\Request;
6
use Laravel\Spark\Http\Controllers\Controller;
7
use Mpociot\CaptainHook\Http\Requests\CreateWebhookRequest;
8
use Mpociot\CaptainHook\Http\Requests\UpdateWebhookRequest;
9
use Mpociot\CaptainHook\Webhook;
10
11
class WebhookController extends Controller
12
{
13
    /**
14
     * Create a new controller instance.
15
     *
16
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct()
19
    {
20
    }
21
22
    /**
23
     * Get all of the webhooks generated by the user.
24
     *
25
     * @return Response
26
     */
27
    public function all(Request $request)
28
    {
29
        return Webhook::where('tenant_id', $this->getTenantId($request))
30
            ->with('lastLog')
31
            ->with('logs')
32
            ->orderBy('created_at', 'desc')
33
            ->get();
34
    }
35
36
    /**
37
     * Create a new webhook for the user.
38
     *
39
     * @param  CreateWebhookRequest  $request
40
     * @return Response
41
     */
42
    public function store(CreateWebhookRequest $request)
43
    {
44
        $hook = Webhook::create([
45
            'url' => $request->url,
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Mpociot\CaptainHo...s\CreateWebhookRequest>. 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...
46
            'tenant_id' => $this->getTenantId($request),
47
            'event' => $request->event,
0 ignored issues
show
Documentation introduced by
The property event does not exist on object<Mpociot\CaptainHo...s\CreateWebhookRequest>. 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...
48
        ]);
49
50
        return response()->json($hook);
0 ignored issues
show
Documentation introduced by
$hook is of type this<Mpociot\CaptainHook\Http\WebhookController>, but the function expects a string|array.

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...
51
    }
52
53
    /**
54
     * Update the given webhook.
55
     *
56
     * @param  UpdateWebhookRequest  $request
57
     * @param  string  $webhookId
58
     * @return Response
59
     */
60
    public function update(UpdateWebhookRequest $request, $webhookId)
61
    {
62
        $webhook = Webhook::where('tenant_id', $this->getTenantId($request))
63
            ->where('id', $webhookId)
64
            ->firstOrFail();
65
66
        $webhook->url = $request->url;
0 ignored issues
show
Bug introduced by
The property url does not seem to exist in Mpociot\CaptainHook\Http...ts\UpdateWebhookRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67
        $webhook->event = $request->event;
0 ignored issues
show
Bug introduced by
The property event does not seem to exist in Mpociot\CaptainHook\Http...ts\UpdateWebhookRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
        $webhook->save();
69
    }
70
71
    /**
72
     * Delete the given webhook.
73
     *
74
     * @param  Request  $request
75
     * @param  string  $webhookId
76
     * @return Response
77
     */
78
    public function destroy(Request $request, $webhookId)
79
    {
80
        Webhook::where('tenant_id', $this->getTenantId($request))
81
            ->where('id', $webhookId)
82
            ->firstOrFail()
83
            ->delete();
84
    }
85
86
    protected function getTenantId(Request $request)
87
    {
88
        return (config('captain_hook.tenant_spark_model', 'User') == 'Team' && isset($request->user()->currentTeam)) ? $request->user()->currentTeam->id : $request->user()->getKey();
89
    }
90
}
91