Completed
Branch develop (305bdf)
by Nate
06:50
created

Read::addCache()   A

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
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.9666
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\TimelineEvent;
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\Resources\ResourceV1;
15
use Psr\Log\LoggerInterface;
16
use Psr\SimpleCache\CacheInterface;
17
18
use Flipbox\Relay\Middleware\SimpleCache as CacheMiddleware;
19
20
class Read extends HttpRelayBuilder
21
{
22
    /**
23
     * The node
24
     */
25
    const NODE = 'integrations';
26
27
    /**
28
     * The resource
29
     */
30
    const RESOURCE = 'timeline/event';
31
32
    /**
33
     * @param string $appId
34
     * @param string $eventTypeId
35
     * @param string $id
36
     * @param AuthorizationInterface $authorization
37
     * @param CacheInterface $cache
38
     * @param LoggerInterface|null $logger
39
     * @param array $config
40
     */
41
    public function __construct(
42
        string $appId,
43
        string $eventTypeId,
44
        string $id,
45
        AuthorizationInterface $authorization,
46
        CacheInterface $cache,
47
        LoggerInterface $logger = null,
48
        $config = []
49
    ) {
50
        parent::__construct($authorization, $logger, $config);
51
52
        $cacheKey = self::RESOURCE . ':' . $appId . ':' . $eventTypeId . ':' . $id;
53
54
        $this->addUri($appId, $eventTypeId, $id, $logger)
55
            ->addCache($cache, $cacheKey, $logger);
56
    }
57
58
    /**
59
     * @param CacheInterface $cache
60
     * @param string $key
61
     * @param LoggerInterface|null $logger
62
     * @return $this
63
     */
64
    protected function addCache(CacheInterface $cache, string $key, LoggerInterface $logger = null)
65
    {
66
        return $this->addBefore('cache', [
67
            'class' => CacheMiddleware::class,
68
            'logger' => $logger ?: $this->getLogger(),
69
            'cache' => $cache,
70
            'key' => $key
71
        ], 'token');
72
    }
73
74
    /**
75
     * @param string $appId
76
     * @param string $eventTypeId
77
     * @param string $id
78
     * @param LoggerInterface|null $logger
79
     * @return $this
80
     */
81
    protected function addUri(string $appId, string $eventTypeId, string $id, LoggerInterface $logger = null)
82
    {
83
        return $this->addBefore('uri', [
84
            'class' => ResourceV1::class,
85
            'node' => self::NODE,
86
            'resource' => $appId . '/' . self::RESOURCE . '/' . $eventTypeId . '/' . $id,
87
            'logger' => $logger ?: $this->getLogger()
88
        ]);
89
    }
90
}
91