Upsert::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/relay-hubspot/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/relay-hubspot
7
 */
8
9
namespace Flipbox\Relay\HubSpot\Builder\Resources\Timeline\Event;
10
11
use Flipbox\Relay\HubSpot\AuthorizationInterface;
12
use Flipbox\Relay\HubSpot\Builder\HttpRelayBuilder;
13
use Flipbox\Relay\HubSpot\Middleware\JsonRequest as JsonMiddleware;
14
use Flipbox\Relay\HubSpot\Middleware\ResourceV1;
15
use Flipbox\Relay\Middleware\ClearSimpleCache as CacheMiddleware;
16
use Psr\Log\LoggerInterface;
17
use Psr\SimpleCache\CacheInterface;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class Upsert extends HttpRelayBuilder
24
{
25
    /**
26
     * The node
27
     */
28
    const NODE = 'integrations';
29
30
    /**
31
     * The resource
32
     */
33
    const RESOURCE = 'timeline/event';
34
35
    /**
36
     * @param string $appId
37
     * @param string $eventTypeId
38
     * @param string $id
39
     * @param array $payload
40
     * @param AuthorizationInterface $authorization
41
     * @param CacheInterface $cache
42
     * @param LoggerInterface|null $logger
43
     * @param array $config
44
     */
45
    public function __construct(
46
        string $appId,
47
        string $eventTypeId,
48
        string $id,
49
        array $payload,
50
        AuthorizationInterface $authorization,
51
        CacheInterface $cache,
52
        LoggerInterface $logger = null,
53
        $config = []
54
    ) {
55
        parent::__construct($authorization, $logger, $config);
56
57
        $cacheKey = self::RESOURCE . ':' . $appId . ':' . $eventTypeId . ':' . $id;
58
59
        $this->addUri($appId, $logger)
60
            ->addPayload($payload, $logger)
61
            ->addCache($cache, $cacheKey, $logger);
62
    }
63
64
    /**
65
     * @param array $payload
66
     * @param LoggerInterface|null $logger
67
     * @return $this
68
     */
69
    protected function addPayload(array $payload, LoggerInterface $logger = null)
70
    {
71
        return $this->addAfter('body', [
72
            'class' => JsonMiddleware::class,
73
            'payload' => $payload,
74
            'logger' => $logger ?: $this->getLogger()
75
        ], 'uri');
76
    }
77
78
    /**
79
     * @param string $appId
80
     * @param LoggerInterface|null $logger
81
     * @return $this
82
     */
83
    protected function addUri(string $appId, LoggerInterface $logger = null)
84
    {
85
        return $this->addBefore('uri', [
86
            'class' => ResourceV1::class,
87
            'method' => 'PUT',
88
            'node' => self::NODE,
89
            'resource' => $appId . '/' . self::RESOURCE,
90
            'logger' => $logger ?: $this->getLogger()
91
        ]);
92
    }
93
94
    /**
95
     * @param CacheInterface $cache
96
     * @param string|null $key
97
     * @param LoggerInterface|null $logger
98
     * @return $this
99
     */
100
    protected function addCache(CacheInterface $cache, string $key = null, LoggerInterface $logger = null)
101
    {
102
        return $this->addAfter('cache', [
103
            'class' => CacheMiddleware::class,
104
            'logger' => $logger ?: $this->getLogger(),
105
            'cache' => $cache,
106
            'key' => $key
107
        ], 'body');
108
    }
109
}
110