Passed
Push — develop ( e52e1a...15535a )
by William
02:01
created

Flagship::requestAllCampaigns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Wcomnisky\Flagship;
6
7
use Symfony\Component\HttpClient\Response\MockResponse;
8
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
9
use Symfony\Contracts\HttpClient\HttpClientInterface;
10
use Symfony\Contracts\HttpClient\ResponseInterface;
11
use Wcomnisky\Flagship\Api\RequestParameters;
12
use Wcomnisky\Flagship\Context\ContextInterface;
13
14
/**
15
 * Class Flagship
16
 * @package Wcomnisky\Flagship
17
 * @see http://developers.flagship.io/api/v1/
18
 */
19
class Flagship
20
{
21
    private const NAMED_PARAM_ENV_ID = '%ENVIRONMENT_ID';
22
    private const NAMED_PARAM_CAMPAIGN_ID = '%CAMPAIGN_ID';
23
24
    private const URL_BASE = 'https://decision-api.flagship.io/v1';
25
    private const URL_ALL_CAMPAIGNS = self::URL_BASE . '/' . self::NAMED_PARAM_ENV_ID . '/campaigns';
26
    private const URL_SINGLE_CAMPAIGN = self::URL_ALL_CAMPAIGNS . '/' . self::NAMED_PARAM_CAMPAIGN_ID;
27
28
    /**
29
     * @var RequestParameters
30
     * @see http://developers.flagship.io/api/v1/#request-parameters
31
     */
32
    private $requestParameters;
33
34
    /**
35
     * @var string The environment ID identifies your account and environment
36
     */
37
    private $environmentId;
38
39
    /**
40
     * @var HttpClientInterface
41
     */
42
    private $httpClient;
43
44
    /**
45
     * Flagship constructor
46
     *
47
     * @param string $environmentId Flagship Environment ID
48
     * @param HttpClientInterface $httpClient
49
     */
50 3
    public function __construct(string $environmentId, HttpClientInterface $httpClient)
51
    {
52 3
        if (empty($environmentId)) {
53 1
            throw new \InvalidArgumentException('Environment ID cannot be empty');
54
        }
55
56 2
        $this->environmentId = $environmentId;
57 2
        $this->httpClient = $httpClient;
58 2
        $this->requestParameters = new RequestParameters();
59 2
    }
60
61
    /**
62
     * Sets the Request Parameters
63
     *
64
     * @param RequestParameters $requestParameters
65
     */
66
    public function setRequestParameters(RequestParameters $requestParameters): void
67
    {
68
        $this->requestParameters = $requestParameters;
69
    }
70
71
    /**
72
     * @param string $visitorId ID of the visitor
73
     * @param string $campaignId The same as Custom ID on their docs
74
     * @param ContextInterface $context
75
     * @return ResponseInterface
76
     * @throws TransportExceptionInterface
77
     */
78 1
    public function requestSingleCampaign(
79
        string $visitorId,
80
        string $campaignId,
81
        ContextInterface $context
82
    ): ResponseInterface {
83
84
        $jsonArray = [
85 1
            'visitor_id' => $visitorId,
86 1
            'context' => $context->getList(),
87 1
            'decision_group' => $this->requestParameters->getDecisionGroup(),
88 1
            'format_response' => $this->requestParameters->isFormatResponseEnabled(),
89 1
            'trigger_hit' => $this->requestParameters->isTriggerHitEnabled()
90
        ];
91
92 1
        $response = $this->httpClient->request(
93 1
            'POST',
94 1
            $this->getSingleCampaignUrl($campaignId),
95
            [
96 1
                'json' => $jsonArray
97
            ]
98
        );
99
100 1
        return $response;
101
    }
102
103
    public function requestAllCampaigns(string $visitorId, ContextInterface $context): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function requestAllCampaigns(string $visitorId, /** @scrutinizer ignore-unused */ ContextInterface $context): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $visitorId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function requestAllCampaigns(/** @scrutinizer ignore-unused */ string $visitorId, ContextInterface $context): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        return new MockResponse();
106
    }
107
108
    /**
109
     * Replaces the named parameter (key) with its correlated value
110
     *
111
     * @param string $source
112
     * @param array $namedParamKVP
113
     * @return string
114
     */
115 1
    private function replaceNamedParameter(string $source, array $namedParamKVP): string
116
    {
117 1
        return str_replace(array_keys($namedParamKVP), array_values($namedParamKVP), $source);
118
    }
119
120 1
    private function getSingleCampaignUrl(string $campaignId): string
121
    {
122 1
        return $this->replaceNamedParameter(self::URL_SINGLE_CAMPAIGN, [
123 1
            self::NAMED_PARAM_ENV_ID => $this->environmentId,
124 1
            self::NAMED_PARAM_CAMPAIGN_ID => $campaignId
125
        ]);
126
    }
127
128
    private function getAllCampaignsUrl(): string
0 ignored issues
show
Unused Code introduced by
The method getAllCampaignsUrl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
129
    {
130
        return $this->replaceNamedParameter(self::URL_ALL_CAMPAIGNS, [
131
            self::NAMED_PARAM_ENV_ID => $this->environmentId,
132
        ]);
133
    }
134
}
135