Completed
Push — master ( 8131b6...c9ac78 )
by Marcel
09:22 queued 05:21
created

TriggerWebhooksJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C handle() 0 43 8
1
<?php
2
namespace Mpociot\CaptainHook\Jobs;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Middleware;
6
use GuzzleHttp\Promise\Promise;
7
use Illuminate\Bus\Queueable;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use Mpociot\CaptainHook\WebhookLog;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
class TriggerWebhooksJob implements ShouldQueue
16
{
17
    use Queueable, InteractsWithQueue, SerializesModels;
18
19
20
    /**
21
     * All the webhooks that should be executed.
22
     *
23
     * @var array|\Illuminate\Support\Collection
24
     */
25
    protected $webhooks;
26
27
    /**
28
     * The event data to be posted to our hooks.
29
     *
30
     * @var mixed
31
     */
32
    protected $eventData;
33
34
    /**
35
     * Create a new job instance.
36
     *
37
     * @param array|\Illuminate\Support\Collection $wekbhooks
0 ignored issues
show
Documentation introduced by
There is no parameter named $wekbhooks. Did you maybe mean $webhooks?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
38
     * @param $eventData
39
     */
40
    public function __construct($webhooks, $eventData)
41
    {
42
        $this->eventData = $eventData;
43
        $this->webhooks = $webhooks;
44
    }
45
46
    /**
47
     * Execute the job.
48
     *
49
     * @return void
50
     */
51
    public function handle()
52
    {
53
        $config = app('Illuminate\Contracts\Config\Repository');
54
        $client = app(Client::class);
55
56
        $logging = $config->get('captain_hook.log.active') && $config->get('queue.driver') != 'sync';
57
58
        foreach ($this->webhooks as $webhook) {
59
            if ($logging) {
60
                if ($config->get('captain_hook.log.storage_quantity') != -1 &&
61
                    $webhook->logs()->count() >= $config->get('captain_hook.log.storage_quantity')) {
62
                    $webhook->logs()->orderBy('updated_at', 'desc')->first()->delete();
63
                }
64
                $log = new WebhookLog([
65
                    'webhook_id' => $webhook[ 'id' ],
66
                    'url' => $webhook[ 'url' ],
67
                ]);
68
                $middleware = Middleware::tap(function (RequestInterface $request, $options) use ($log) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
69
                    $log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null;
0 ignored issues
show
Documentation introduced by
The property payload_format does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
70
                    $log->payload = $request->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property payload does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
71
                }, function ($request, $options, Promise $response) use ($log) {
72
                    $response->then(function (ResponseInterface $response) use ($log) {
73
                        $log->status = $response->getStatusCode();
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
74
                        $log->response = $response->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
75
                        $log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null;
0 ignored issues
show
Documentation introduced by
The property response_format does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Documentation introduced by
The property payload_format does not exist on object<Mpociot\CaptainHook\WebhookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
76
77
                        $log->save();
78
                    });
79
                });
80
81
                $client->post($webhook[ 'url' ], [
82
                    'body' => $this->eventData,
83
                    'handler' => $middleware($client->getConfig('handler')),
84
                ]);
85
            } else {
86
                $client->postAsync($webhook[ 'url' ], [
87
                    'body' => $this->eventData,
88
                    'verify' => false,
89
                    'future' => true,
90
                ]);
91
            }
92
        }
93
    }
94
}
95