Completed
Pull Request — master (#26)
by
unknown
07:04
created

Verifier   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 14
c 7
b 0
f 2
lcom 1
cbo 4
dl 0
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A verify() 0 12 2
A createRequest() 0 6 1
A getServiceEndpoint() 0 6 2
C buildQuery() 0 28 8
1
<?php
2
3
namespace Mdb\PayPal\Ipn;
4
5
use Http\Client\HttpClient;
6
use Http\Message\MessageFactory;
7
8
class Verifier
9
{
10
    const PAYPAL_ENDPOINT = 'https://www.paypal.com/cgi-bin/webscr';
11
    const PAYPAL_SANDBOX_ENDPOINT = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
12
    const STATUS_KEYWORD_VERIFIED = 'VERIFIED';
13
    const STATUS_KEYWORD_INVALID = 'INVALID';
14
15
    /**
16
     * @var HttpClient
17
     */
18
    private $httpClient;
19
20
    /**
21
     * @var MessageFactory
22
     */
23
    private $messageFactory;
24
25
    /**
26
     * @var bool
27
     */
28
    private $useSandbox;
29
30
    public function __construct(HttpClient $httpClient, MessageFactory $messageFactory, $useSandbox = false)
31
    {
32
        $this->httpClient = $httpClient;
33
        $this->messageFactory = $messageFactory;
34
        $this->useSandbox = $useSandbox;
35
    }
36
37
    /**
38
     * @param array $datas
39
     *
40
     * @return bool
41
     *
42
     * @throws \UnexpectedValueException
43
     */
44
    public function verify(array $datas)
45
    {
46
        $body = $this->httpClient->sendRequest($this->createRequest($datas))->getBody()->getContents();
47
48
        $pattern = sprintf('/(%s|%s)/', self::STATUS_KEYWORD_VERIFIED, self::STATUS_KEYWORD_INVALID);
49
50
        if (!preg_match($pattern, $body)) {
51
            throw new \UnexpectedValueException(sprintf('Unexpected verification status encountered: %s', $body));
52
        }
53
54
        return $body === self::STATUS_KEYWORD_VERIFIED;
55
    }
56
57
    /**
58
     * @param array $datas
59
     *
60
     * @return \Psr\Http\Message\RequestInterface
61
     */
62
    protected function createRequest(array $datas)
63
    {
64
        $body = $this->buildQuery(['cmd' => '_notify-validate'] + $datas);
65
66
        return $this->messageFactory->createRequest('POST', $this->getServiceEndpoint(), [], $body);
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getServiceEndpoint()
73
    {
74
        return $this->useSandbox ?
75
            self::PAYPAL_SANDBOX_ENDPOINT :
76
            self::PAYPAL_ENDPOINT;
77
    }
78
79
    private function buildQuery(array $params)
80
    {
81
        if (!$params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
            return '';
83
        }
84
85
        $qs = '';
86
        foreach ($params as $k => $v) {
87
            $k = urlencode($k);
88
            if (!is_array($v)) {
89
                $qs .= $k;
90
                if ($v !== null) {
91
                    $qs .= '='.urlencode($v);
92
                }
93
                $qs .= '&';
94
            } else {
95
                foreach ($v as $vv) {
96
                    $qs .= $k;
97
                    if ($vv !== null) {
98
                        $qs .= '='.urlencode($vv);
99
                    }
100
                    $qs .= '&';
101
                }
102
            }
103
        }
104
105
        return $qs ? (string) substr($qs, 0, -1) : '';
106
    }
107
}
108