Completed
Push — master ( 819413...b7d055 )
by Nate
15:40 queued 02:16
created

ListByDomain::addPayload()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 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\Company;
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\ResourceV2;
15
use Flipbox\Relay\Middleware\SimpleCache as CacheMiddleware;
16
use Psr\Log\LoggerInterface;
17
use Psr\SimpleCache\CacheInterface;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.1.0
22
 */
23
class ListByDomain extends HttpRelayBuilder
24
{
25
    /**
26
     * The node
27
     */
28
    const NODE = 'companies';
29
30
    /**
31
     * The resource
32
     */
33
    const RESOURCE = 'domains';
34
35
    /**
36
     * @param string $domain
37
     * @param array $payload
38
     * @param CacheInterface $cache
39
     * @param AuthorizationInterface $authorization
40
     * @param LoggerInterface|null $logger
41
     * @param array $config
42
     */
43
    public function __construct(
44
        string $domain,
45
        array $payload,
46
        AuthorizationInterface $authorization,
47
        CacheInterface $cache,
48
        LoggerInterface $logger = null,
49
        $config = []
50
    )
51
    {
52
        parent::__construct($authorization, $logger, $config);
53
54
        $cacheKey = self::RESOURCE . ':' . $domain . ':companies:' . md5(serialize($payload));
55
56
        $this->addUri($domain, $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 CacheInterface $cache
77
     * @param string $key
78
     * @param LoggerInterface|null $logger
79
     * @return $this
80
     */
81
    protected function addCache(CacheInterface $cache, string $key, LoggerInterface $logger = null)
82
    {
83
        return $this->addAfter('cache', [
84
            'class' => CacheMiddleware::class,
85
            'logger' => $logger ?: $this->getLogger(),
86
            'cache' => $cache,
87
            'key' => $key
88
        ], 'body');
89
    }
90
91
    /**
92
     * @param string $domain
93
     * @param LoggerInterface|null $logger
94
     * @return $this
95
     */
96
    protected function addUri(string $domain, LoggerInterface $logger = null)
97
    {
98
        return $this->addBefore('uri', [
99
            'class' => ResourceV2::class,
100
            'method' => 'POST',
101
            'node' => self::NODE,
102
            'resource' => self::RESOURCE . '/' . $domain . '/companies',
103
            'logger' => $logger ?: $this->getLogger()
104
        ]);
105
    }
106
}
107