Update::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 0
cts 14
cp 0
rs 9.7333
cc 1
nc 1
nop 6
crap 2
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\Contact;
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 Update extends HttpRelayBuilder
24
{
25
    /**
26
     * The node
27
     */
28
    const NODE = 'contacts';
29
30
    /**
31
     * The resource
32
     */
33
    const RESOURCE = 'contact';
34
35
    /**
36
     * Upsert constructor.
37
     * @param string $identifier
38
     * @param array $payload
39
     * @param AuthorizationInterface $authorization
40
     * @param CacheInterface $cache
41
     * @param LoggerInterface|null $logger
42
     * @param array $config
43
     */
44
    public function __construct(
45
        string $identifier,
46
        array $payload,
47
        AuthorizationInterface $authorization,
48
        CacheInterface $cache,
49
        LoggerInterface $logger = null,
50
        $config = []
51
    ) {
52
        parent::__construct($authorization, $logger, $config);
53
54
        $cacheKey = self::RESOURCE . ':' . $identifier;
55
56
        $this->addUri($identifier, $logger)
57
            ->addPayload($payload, $logger)
58
            ->addCache($cache, $cacheKey, $logger);
59
    }
60
61
    /**
62
     * @param array $payload
63
     * @param LoggerInterface|null $logger
64
     * @return $this
65
     */
66
    protected function addPayload(array $payload, LoggerInterface $logger = null)
67
    {
68
        return $this->addAfter('body', [
69
            'class' => JsonMiddleware::class,
70
            'payload' => $payload,
71
            'logger' => $logger ?: $this->getLogger()
72
        ], 'uri');
73
    }
74
75
    /**
76
     * @param string $id
77
     * @param LoggerInterface|null $logger
78
     * @return $this
79
     */
80
    protected function addUri(string $id, LoggerInterface $logger = null)
81
    {
82
        return $this->addBefore('uri', [
83
            'class' => ResourceV1::class,
84
            'method' => 'POST',
85
            'node' => self::NODE,
86
            'resource' => self::RESOURCE . '/vid/' . $id . '/profile',
87
            'logger' => $logger ?: $this->getLogger()
88
        ]);
89
    }
90
91
    /**
92
     * @param CacheInterface $cache
93
     * @param string|null $key
94
     * @param LoggerInterface|null $logger
95
     * @return $this
96
     */
97
    protected function addCache(CacheInterface $cache, string $key = null, LoggerInterface $logger = null)
98
    {
99
        return $this->addAfter('cache', [
100
            'class' => CacheMiddleware::class,
101
            'logger' => $logger ?: $this->getLogger(),
102
            'cache' => $cache,
103
            'key' => $key
104
        ], 'body');
105
    }
106
}
107