IpagResource::setIpag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Ipag\Classes;
4
5
use Ipag\Classes\Http\AuthenticableHttpInterface;
6
use Ipag\Classes\Http\CurlOnlyPostHttpClient;
7
use Ipag\Ipag;
8
use stdClass;
9
10
abstract class IpagResource extends BaseResource
11
{
12
    /**
13
     * @var Ipag
14
     */
15
    protected $ipag;
16
17
    /**
18
     * @var AuthenticableHttpInterface
19
     */
20
    protected $onlyPostClient;
21
22
    /**
23
     * @var Contracts\Operationable
24
     */
25
    protected $apiService;
26
27
    abstract public function populate(stdClass $response);
28
29
    public function __construct(Ipag $ipag)
30
    {
31
        $this->setOnlyPostClient(new CurlOnlyPostHttpClient());
32
        $this->setIpag($ipag);
33
    }
34
35
    /**
36
     * @return AuthenticableHttpInterface
37
     */
38
    public function getOnlyPostClient()
39
    {
40
        return $this->onlyPostClient;
41
    }
42
43
    /**
44
     * @param AuthenticableHttpInterface $onlyPostClient
45
     */
46
    public function setOnlyPostClient(AuthenticableHttpInterface $onlyPostClient)
47
    {
48
        $this->onlyPostClient = $onlyPostClient;
49
50
        return $this;
51
    }
52
53
    public function sendHttpRequest($endpoint, $parameters)
54
    {
55
        $onlyPostClient = $this->getOnlyPostClient();
56
        $response = $onlyPostClient(
57
            $endpoint,
58
            $this->getIpagHeaders(),
59
            $parameters
60
        );
61
62
        return $this->checkIfIsValidXmlAndReturnStdClass($response);
63
    }
64
65
    private function checkIfIsValidXmlAndReturnStdClass($response)
66
    {
67
        $xmlService = new Services\XmlService();
68
69
        $simpleXmlObject = $xmlService->validate($response);
70
71
        return $xmlService->xmlToStdClass($simpleXmlObject);
72
    }
73
74
    private function getIpagHeaders()
75
    {
76
        return [
77
            'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
78
            'Accept'       => 'application/xml',
79
        ];
80
    }
81
82
    /**
83
     * @return Ipag
84
     */
85
    public function getIpag()
86
    {
87
        return $this->ipag;
88
    }
89
90
    /**
91
     * @param Ipag $ipag
92
     */
93
    public function setIpag(Ipag $ipag)
94
    {
95
        $this->ipag = $ipag;
96
97
        return $this;
98
    }
99
}
100