PayplugAccountLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Alcalyn\PayplugBundle\Services;
4
5
use Symfony\Component\Yaml\Dumper;
6
use Symfony\Component\Yaml\Parser;
7
use Alcalyn\PayplugBundle\Exceptions\PayplugException;
8
9
class PayplugAccountLoader
10
{
11
    /**
12
     * Url of Payplug account autoconfig
13
     * 
14
     * @var string
15
     */
16
    const PAYPLUG_AUTOCONFIG_URL = 'https://www.payplug.fr/portal/ecommerce/autoconfig';
17
    
18
    /**
19
     * Url of Payplug test account autoconfig
20
     * 
21
     * @var string
22
     */
23
    const PAYPLUG_AUTOCONFIG_URL_TEST = 'https://www.payplug.fr/portal/test/ecommerce/autoconfig';
24
    
25
    /**
26
     * Kernel root dir
27
     * 
28
     * @var string
29
     */
30
    private $rootDir;
31
    
32
    /**
33
     * @param string $rootDir
34
     */
35
    public function __construct($rootDir)
36
    {
37
        $this->rootDir = $rootDir;
38
    }
39
    
40
    /**
41
     * Load Payplug account parameters and set them into parameters.yml
42
     * 
43
     * @param string $mail
44
     * @param string $pass
45
     * @param boolean $test
46
     * @param boolean $noprod
47
     * 
48
     * @throws PayplugException on curl or authentication error
49
     */
50
    public function loadPayplugParameters($mail, $pass, $test = false, $noprod = false)
51
    {
52
        $result = $this->curlPayplugRequest($mail, $pass, $test);
53
        
54
        $params = json_decode($result);
55
        $status = intval($params->status);
56
        unset($params->status);
57
        
58
        if (200 !== $status) {
59
            $errorName = $this->errorStatusResolver($status);
60
            
61
            throw new PayplugException(
62
                'Payplug response returned status '.$status.' '.$errorName.'. Unable to continue.'
63
            );
64
        }
65
        
66
        $payplugAccount = array();
67
        
68
        if (!$noprod) {
69
            foreach ($params as $key => $value) {
70
                $payplugAccount['parameters']['payplug_account_'.$key] = $value;
71
            }
72
        }
73
        if ($test) {
74
            $payplugAccount['parameters']['payplug_sandbox_account_url'] = $params->url;
75
            $payplugAccount['parameters']['payplug_sandbox_account_yourPrivateKey'] = $params->yourPrivateKey;
76
        }
77
        
78
        $this->editParameters($payplugAccount);
79
    }
80
    
81
    /**
82
     * Make a curl authenticated request to Payplug to get account parameters.
83
     * Warning: CURLOPT_SSL_VERIFYPEER set to false, so there is not TLS certificate check.
84
     * 
85
     * @param string $mail
86
     * @param string $pass
87
     * @param boolean $test
88
     * 
89
     * @throws PayplugException on curl error
90
     */
91
    public function curlPayplugRequest($mail, $pass, $test = false)
92
    {
93
        $options = array(
94
            CURLOPT_RETURNTRANSFER => true,
95
            CURLOPT_USERPWD => $mail.':'.$pass,
96
            CURLOPT_HTTPHEADER => array('Content-type: application/json'),
97
            CURLOPT_SSL_VERIFYPEER => false,
98
        );
99
        
100
        $curl = curl_init($this->getAutoconfigUrl($test));
101
        curl_setopt_array($curl, $options);
102
        $result = curl_exec($curl);
103
        
104
        if (false === $result) {
105
            throw new PayplugException('Curl error: '.curl_error($curl));
106
        }
107
        
108
        return $result;
109
    }
110
    
111
    /**
112
     * Add or edit parameters in parameters.yml
113
     * 
114
     * @param array $parametersArray
115
     */
116
    public function editParameters(array $parametersArray)
117
    {
118
        $parametersFile = $this->rootDir.'/config/parameters.yml';
119
        
120
        $parser = new Parser();
121
        $dumper = new Dumper();
122
        
123
        $parameters = $parser->parse(file_get_contents($parametersFile));
124
        $newFileContent = $dumper->dump(array_replace_recursive($parameters, $parametersArray), 2);
125
        
126
        file_put_contents($parametersFile, $newFileContent);
127
    }
128
    
129
    /**
130
     * Return autoconfig for environment
131
     * 
132
     * @param boolean $test
133
     * 
134
     * @return string
135
     */
136
    public function getAutoconfigUrl($test = false)
137
    {
138
        if ($test) {
139
            return self::PAYPLUG_AUTOCONFIG_URL_TEST;
140
        } else {
141
            return self::PAYPLUG_AUTOCONFIG_URL;
142
        }
143
    }
144
    
145
    /**
146
     * Return a detailled error message from status code
147
     * 
148
     * @param int $status
149
     * 
150
     * @return string
151
     */
152
    public function errorStatusResolver($status)
153
    {
154
        switch ($status) {
155
            case 200:
156
                return 'Success';
157
                
158
            case 401:
159
                return 'Authentication error';
160
            
161
            default:
162
                return 'Unexpected status';
163
        }
164
    }
165
}
166