Add::addCache()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 1
nop 3
crap 6
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\Company\Contacts;
10
11
use Flipbox\Relay\HubSpot\AuthorizationInterface;
12
use Flipbox\Relay\HubSpot\Builder\HttpRelayBuilder;
13
use Flipbox\Relay\HubSpot\Middleware\ResourceV2;
14
use Flipbox\Relay\Middleware\ClearSimpleCache as CacheMiddleware;
15
use Psr\Log\LoggerInterface;
16
use Psr\SimpleCache\CacheInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Add extends HttpRelayBuilder
23
{
24
    /**
25
     * The node
26
     */
27
    const NODE = 'companies';
28
29
    /**
30
     * The resource
31
     */
32
    const RESOURCE = 'companies';
33
34
    /**
35
     * Upsert constructor.
36
     * @param string $companyId
37
     * @param string $contactId
38
     * @param AuthorizationInterface $authorization
39
     * @param CacheInterface $cache
40
     * @param LoggerInterface|null $logger
41
     * @param array $config
42
     */
43
    public function __construct(
44
        string $companyId,
45
        string $contactId,
46
        AuthorizationInterface $authorization,
47
        CacheInterface $cache,
48
        LoggerInterface $logger = null,
49
        $config = []
50
    ) {
51
        parent::__construct($authorization, $logger, $config);
52
53
        $cacheKey = self::RESOURCE . ':' . $companyId . ':contacts';
54
55
        $this->addUri($companyId, $contactId, $logger)
56
            ->addCache($cache, $cacheKey, $logger);
57
    }
58
59
    /**
60
     * @param string $companyId
61
     * @param string $contactId
62
     * @param LoggerInterface|null $logger
63
     * @return $this
64
     */
65
    protected function addUri(string $companyId, string $contactId, LoggerInterface $logger = null)
66
    {
67
        return $this->addBefore('uri', [
68
            'class' => ResourceV2::class,
69
            'method' => 'PUT',
70
            'node' => self::NODE,
71
            'resource' => self::RESOURCE . '/' . $companyId . '/contacts/' . $contactId,
72
            'logger' => $logger ?: $this->getLogger()
73
        ]);
74
    }
75
76
    /**
77
     * @param CacheInterface $cache
78
     * @param string|null $key
79
     * @param LoggerInterface|null $logger
80
     * @return $this
81
     */
82
    protected function addCache(CacheInterface $cache, string $key = null, LoggerInterface $logger = null)
83
    {
84
        return $this->addAfter('cache', [
85
            'class' => CacheMiddleware::class,
86
            'logger' => $logger ?: $this->getLogger(),
87
            'cache' => $cache,
88
            'key' => $key
89
        ], 'body');
90
    }
91
}
92