Completed
Pull Request — master (#14)
by
unknown
84:18 queued 44:32
created

TriggerWebhooksJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C handle() 0 42 8
1
<?php
2
namespace Mpociot\CaptainHook\Jobs;
3
4
5
use Carbon\Carbon;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Middleware;
8
use GuzzleHttp\Promise\Promise;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Contracts\Queue\ShouldQueue;
11
use Illuminate\Queue\InteractsWithQueue;
12
use Illuminate\Queue\SerializesModels;
13
use Mpociot\CaptainHook\CaptainHookLog;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
class TriggerWebhooksJob implements ShouldQueue
18
{
19
    use Queueable, InteractsWithQueue, SerializesModels;
20
21
22
    /**
23
     * All the webhooks that should be executed.
24
     *
25
     * @var array|\Illuminate\Support\Collection
26
     */
27
    protected $webhooks;
28
29
    /**
30
     * The event data to be posted to our hooks.
31
     *
32
     * @var mixed
33
     */
34
    protected $eventData;
35
36
    /**
37
     * Create a new job instance.
38
     *
39
     * @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...
40
     * @param $eventData
41
     */
42
    public function __construct($webhooks, $eventData)
43
    {
44
        $this->eventData = $eventData;
45
        $this->webhooks = $webhooks;
46
    }
47
48
    /**
49
     * Execute the job.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        $config = app('Illuminate\Contracts\Config\Repository');
56
        $client = app(Client::class);
57
58
        if (($logging = $config->get('captain_hook.log.active') && $config->get('queue.driver') != 'sync') &&
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $logging = ($config->get...eue.driver') != 'sync'), Probably Intended Meaning: ($logging = $config->get...ueue.driver') != 'sync'
Loading history...
59
            $config->get('captain_hook.log.storage_time') != -1) {
60
            CaptainHookLog::where('updated_at', '<', Carbon::now()->subHours($config->get('captain_hook.log.storage_time')))->delete();
61
        }
62
63
        foreach ($this->webhooks as $webhook) {
64
            if ($logging) {
65
                $log = new CaptainHookLog([
66
                    'webhook_id'     => $webhook[ 'id' ],
67
                    'url'            => $webhook[ 'url' ],
68
                ]);
69
                $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...
70
                    $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\CaptainHookLog>. 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
                    $log->payload = $request->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property payload does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. 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...
72
                }, function ($request, $options, Promise $response) use ($log) {
73
                    $response->then(function (ResponseInterface $response) use ($log) {
74
                        $log->status = $response->getStatusCode();
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. 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 = $response->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. 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
                        $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\CaptainHookLog>. 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\CaptainHookLog>. 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...
77
78
                        $log->save();
79
                    });
80
                });
81
82
                $client->post($webhook[ 'url' ], [
83
                    'body' => $this->eventData,
84
                    'handler' => $middleware($client->getConfig('handler')),
85
                ]);
86
            } else {
87
                $client->postAsync($webhook[ 'url' ], [
88
                    'body' => $this->eventData,
89
                    'verify' => false,
90
                    'future' => true,
91
                ]);
92
            }
93
        }
94
    }
95
}