Redirect   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getLink() 0 3 1
A setItems() 0 3 1
A setReference() 0 4 1
A __construct() 0 3 1
A getEnv() 0 3 1
A setEnv() 0 4 1
A getItems() 0 3 1
A loadData() 0 18 2
A getReference() 0 6 2
A getCode() 0 6 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: igor
5
 * Date: 09/06/18
6
 * Time: 14:42
7
 */
8
9
namespace AdminWeb\PayerPagSeguro\Payment;
10
11
12
use AdminWeb\Payer\EnvInterface;
13
use AdminWeb\Payer\Itemable\ItemList;
14
use GuzzleHttp\Client;
15
16
class Redirect
17
{
18
    private $items = [], $env, $reference, $code;
19
20
    public function __construct(EnvInterface $env)
21
    {
22
        $this->setEnv($env);
23
    }
24
25
    /**
26
     * @param mixed $env
27
     * @return Redirect
28
     */
29
    public function setEnv(EnvInterface $env)
30
    {
31
        $this->env = $env;
32
        return $this;
33
    }
34
35
    public function getLink()
36
    {
37
        return str_replace('{{codigo-checkout}}', $this->getCode()->code, 'https://pagseguro.uol.com.br/v2/checkout/payment.html?code={{codigo-checkout}}');
38
    }
39
40
    protected function getCode()
41
    {
42
        $c = new Client([
43
            'base_uri' => $this->getEnv()->getUri()
44
        ]);
45
        return simplexml_load_string($c->post('/v2/checkout', ['form_params' => $this->loadData()])->getBody()->getContents());
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51
    public function getEnv()
52
    {
53
        return $this->env;
54
    }
55
56
    protected function loadData()
57
    {
58
        $data = [
59
            'email' => $this->getEnv()->getCredential(),
60
            'token' => $this->getEnv()->getToken(),
61
            'currency' => 'BRL'
62
        ];
63
        $items = $this->getItems()->getItem();
64
        $itemsCount = $this->getItems()->count();
65
66
        for ($i = 1; $i <= $itemsCount; $i++) {
67
            $data["itemId{$i}"] = $items->offsetGet($i - 1)->getidItem();
68
            $data["itemDescription{$i}"] = $items->offsetGet($i - 1)->getName();
69
            $data["itemAmount{$i}"] = $items->offsetGet($i - 1)->getAmount();
70
            $data["itemQuantity{$i}"] = $items->offsetGet($i - 1)->getQuantity();
71
        }
72
        $data['reference'] = $this->getReference();
73
        return $data;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getItems()
80
    {
81
        return $this->items;
82
    }
83
84
    /**
85
     * @param array $items
86
     */
87
    public function setItems(ItemList $items)
88
    {
89
        $this->items = $items;
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function getReference()
96
    {
97
        if (is_null($this->reference)) {
98
            throw new PaymentException('The Payment need have a reference!');
99
        }
100
        return $this->reference;
101
    }
102
103
    /**
104
     * @param mixed $reference
105
     * @return Redirect
106
     */
107
    public function setReference($reference)
108
    {
109
        $this->reference = $reference;
110
        return $this;
111
    }
112
113
114
}