StripeCheckoutUrlBuilder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 73
ccs 32
cts 32
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 13 3
A prepareSuccessUrl() 0 5 1
A prepareUrls() 0 7 1
A __construct() 0 3 1
A withSessionId() 0 5 1
A prepareCancelUrl() 0 5 1
A make() 0 3 1
A useResultStatus() 0 5 1
1
<?php
2
3
namespace CashierUtils\Checkout;
4
5
class StripeCheckoutUrlBuilder
6
{
7
    public static string $sessionKey      = 'checkout_session';
8
    public static string $resultStatusKey = 'payment_status';
9
10
    public static string $statusSuccess  = 'success';
11
    public static string $statusCanceled = 'canceled';
12
13
    protected string $url;
14
15
    protected bool $withSessionId   = false;
16
    protected ?string $resultStatus = null;
17
18 5
    protected function __construct(string $url)
19
    {
20 5
        $this->url = $url;
21
    }
22
23 5
    public static function make(string $url)
24
    {
25 5
        return new static($url);
26
    }
27
28 3
    public function withSessionId(bool $withSessionId = true): static
29
    {
30 3
        $this->withSessionId = $withSessionId;
31
32 3
        return $this;
33
    }
34
35 5
    public function useResultStatus(?string $resultStatus = null): static
36
    {
37 5
        $this->resultStatus = $resultStatus;
38
39 5
        return $this;
40
    }
41
42 5
    public function url(): string
43
    {
44 5
        $query = parse_url($this->url, PHP_URL_QUERY);
45 5
        parse_str($query, $query);
0 ignored issues
show
Bug introduced by
$query of type string is incompatible with the type array expected by parameter $result of parse_str(). ( Ignorable by Annotation )

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

45
        parse_str($query, /** @scrutinizer ignore-type */ $query);
Loading history...
46 5
        if ($this->withSessionId) {
47 3
            $query[static::$sessionKey] = 'CHECKOUT_SESSION_ID_TO_REPLACE';
48
        }
49 5
        if ($this->resultStatus) {
50 5
            $query[static::$resultStatusKey] = $this->resultStatus;
51
        }
52 5
        $newUrl = strtok($this->url, '?') . '?' . http_build_query($query);
53
54 5
        return str_replace('CHECKOUT_SESSION_ID_TO_REPLACE', '{CHECKOUT_SESSION_ID}', $newUrl);
55
    }
56
57 2
    public static function prepareSuccessUrl(string $url, ?string $resultStatus = null): string
58
    {
59 2
        return static::make($url)->withSessionId()
60 2
            ->useResultStatus($resultStatus ?? static::$statusSuccess)
61 2
            ->url();
62
    }
63
64 2
    public static function prepareCancelUrl(string $url, ?string $resultStatus = null): string
65
    {
66 2
        return static::make($url)
67 2
            ->useResultStatus($resultStatus ?? static::$statusCanceled)
68 2
            ->url();
69
    }
70
71 1
    public static function prepareUrls(string $successUrl, ?string $cancelUrl = null): array
72
    {
73 1
        $cancelUrl = $cancelUrl ?? $successUrl;
74
75 1
        return [
76 1
            'success_url' => static::prepareSuccessUrl($successUrl),
77 1
            'cancel_url'  => static::prepareCancelUrl($cancelUrl),
78 1
        ];
79
    }
80
}
81